ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hello, I was writting this code and i did the last part with
$max = $numbers[0];
$max= $_>$max ? $_ : $max foreach (@numbers);
print ("the biggest number is $max\n");
now im trying to do it using if then else.. but i get errors.
any ideas??
Code:
#!/usr/bin/perl
@numbers = ("$num1, $num2, $num3");
print ("Enter the first number? ");
$numbers[0] = <STDIN>;
print ("Enter the second number? ");
$numbers[1]= <STDIN>;
print ("Enter the third number? ");
$numbers[2]= <STDIN>;
@correctly_sorted_numbers = sort {$a <=> $b} @numbers;
print @correctly_sorted_numbers;
$sum = ($numbers[0] + $numbers[1] + $numbers[2]);
$prod= ($numbers[0] * $numbers[1] * $numbers[2]);
print ("First number was $numbers[0]");
print ("Second number was $numbers[1]");
print ("Third number was $numbers[2]");
print ("The Sum is $sum\n");
print ("The product is $prod\n");
if $numbers[0] > $numbers[1]
{
print ("Largest Number is $numbers[0]" );
}
else
if $numbers[1]<$numbers[0]
{
print ("Largest Number is $numbers[1]" );
}
else
if $numbers[1]>$numbers[2]
{
print ("Largest Number is $numbers[1]" );
else
if $numbers[2]<$numbers[1]
{
print ("Largest Number is $numbers[2]" );
}
end If
end If
endif
endif
It's called "elseif" in Perl - written in one word. (See perldoc perlsyn)
Code:
if ($foo eq "bar") {
do stuff;
} elseif ($foo eq "foo") {
do something else;
} else {
catch everything else;
}
Beginning with Perl 5.10, there's also a switch statement - if that suits you better. See perldoc perlsyn for details (it goes like given - when - default). You'll have to enable it with use.
It's called "elseif" in Perl - written in one word. (See perldoc perlsyn)
Code:
if ($foo eq "bar") {
do stuff;
} elseif ($foo eq "foo") {
do something else;
} else {
catch everything else;
}
Beginning with Perl 5.10, there's also a switch statement - if that suits you better. See perldoc perlsyn for details (it goes like given - when - default). You'll have to enable it with use.
Code:
#!/usr/bin/perl
@numbers = ("$num1, $num2, $num3");
print ("Enter the first number? ");
$numbers[0] = <STDIN>;
print ("Enter the second number? ");
$numbers[1]= <STDIN>;
print ("Enter the third number? ");
$numbers[2]= <STDIN>;
@correctly_sorted_numbers = sort {$a <=> $b} @numbers;
print @correctly_sorted_numbers;
$sum = ($numbers[0] + $numbers[1] + $numbers[2]);
$prod= ($numbers[0] * $numbers[1] * $numbers[2]);
print ("First number was $numbers[0]");
print ("Second number was $numbers[1]");
print ("Third number was $numbers[2]");
print ("The Sum is $sum\n");
print ("The product is $prod\n");
if ($numbers[0] > $numbers[1])
{
print ("Largest Number is $numbers[0]" );
}
elseif
if ($numbers[1]<$numbers[0])
{
print ("Largest Number is $numbers[1]" );
}
elseif
if ($numbers[1]>$numbers[2])
{
print ("Largest Number is $numbers[1]" );
elseif
if ($numbers[2]<$numbers[1])
{
print ("Largest Number is $numbers[2]");
}
endIf
endIf
endif
endif
#!/usr/bin/perl
# My name
# Program name
# Purpose
@numbers = ("$num1, $num2, $num3");
print ("Enter the first number? ");
$numbers[0] = <STDIN>;
print ("Enter the second number? ");
$numbers[1]= <STDIN>;
print ("Enter the third number? ");
$numbers[2]= <STDIN>;
@correctly_sorted_numbers = sort {$a <=> $b} @numbers;
print @correctly_sorted_numbers;
$sum = ($numbers[0] + $numbers[1] + $numbers[2]);
$prod= ($numbers[0] * $numbers[1] * $numbers[2]);
print ("First number was $numbers[0]");
print ("Second number was $numbers[1]");
print ("Third number was $numbers[2]");
print ("The Sum is $sum\n");
print ("The product is $prod\n");
if ($numbers[0] > $numbers[1])
{
print ("Largest Number is $numbers[0]");
}
elsif ($numbers[1] < $numbers[0])
{
print ("Largest Number is $numbers[1]");
}
elsif ($numbers[1]>$numbers[2])
{
print ("Largest Number is $numbers[1]" );
}
elsif($numbers[2]<$numbers[1])
{
print ("Largest Number is $numbers[2]" );
}
endIf
endIf
endif
endif
-bash-3.00$ perl asng4.pl
Enter the first number? 1
Enter the second number? 2
Enter the third number? 3
1
2
3
First number was 1
Second number was 2
Third number was 3
The Sum is 6
The product is 6
Can't locate object method "endif" via package "endif" (perhaps you forgot to load "endif"?) at asng4.pl line 39, <STDIN> line 3.
1) You should always include warnings and strict.
2) You should chomp off the newlines from the numbers you get from STDIN.
3) There is no 'endif' in Perl. (Are you thinking of Bash?)
4) You don't need parentheses for such simple print statements.
5) The logic of your test for the largest number is faulty. What you wrote above is "If the first number is bigger than the second, then the first number is the largest." But you can't know which is the largest of three until you check them all. Also, you first test is $numbers[0] is larger than $numbers[1] and then you check if $numbers[1] is smaller than $numbers[0]. Those two tests do exactly the same thing in the opposite direction.
I think you were trying to use a "high-water mark algorithm". Here's my quick version of something like that for you.
Code:
#!/usr/bin/perl
use warnings; # Always include warnings
use strict; # Always enforce strict
my @numbers; # No quotes wanted here; variables not needed
print "Enter the first number? ";
chomp( $numbers[0] = <STDIN> ); # Read in number, remove newline
print "Enter the second number? ";
chomp( $numbers[1] = <STDIN> ); # Do it again
print "Enter the third number? ";
chomp( $numbers[2] = <STDIN> ); # Yup, again
@numbers = sort {$a <=> $b} @numbers;
print "Here they are sorted: @numbers\n";
my $sum = $numbers[0] + $numbers[1] + $numbers[2];
my $prod = $numbers[0] * $numbers[1] * $numbers[2];
print "First number was $numbers[0]\n";
print "Second number was $numbers[1]\n";
print "Third number was $numbers[2]\n";
print "The sum is $sum\n";
print "The product is $prod\n";
my $max = $numbers[0]; # Assign the first number as $max
foreach my $number (1..$#numbers) { # $#numbers allows for more than three
if ( $numbers[$number] > $max ) { # Check other numbers against $max
$max = $numbers[$number]; # Assign to $max if number is bigger than current $max
}
}
print "The largest number is $max\n";
# You can use $array[-1] to give you the last item in an @array
# This array is sorted numerically, so the last number should be largest anyhow
print "Since they're sorted, the largest number is $numbers[-1]\n";
Last edited by Telemachos; 12-13-2008 at 08:25 PM.
1) You should always include warnings and strict.
2) You should chomp off the newlines from the numbers you get from STDIN.
3) There is no 'endif' in Perl. (Are you thinking of Bash?)
4) You don't need parentheses for such simple print statements.
5) The logic of your test for the largest number is faulty. What you wrote above is "If the first number is bigger than the second, then the first number is the largest." But you can't know which is the largest of three until you check them all. Also, you first test is $numbers[0] is larger than $numbers[1] and then you check if $numbers[1] is smaller than $numbers[0]. Those two tests do exactly the same thing in the opposite direction.
I think you were trying to use a "high-water mark algorithm". Here's my quick version of something like that for you.
Code:
#!/usr/bin/perl
use warnings; # Always include warnings
use strict; # Always enforce strict
my @numbers; # No quotes wanted here; variables not needed
print "Enter the first number? ";
chomp( $numbers[0] = <STDIN> ); # Read in number, remove newline
print "Enter the second number? ";
chomp( $numbers[1] = <STDIN> ); # Do it again
print "Enter the third number? ";
chomp( $numbers[2] = <STDIN> ); # Yup, again
@numbers = sort {$a <=> $b} @numbers;
print "Here they are sorted: @numbers\n";
my $sum = $numbers[0] + $numbers[1] + $numbers[2];
my $prod = $numbers[0] * $numbers[1] * $numbers[2];
print "First number was $numbers[0]\n";
print "Second number was $numbers[1]\n";
print "Third number was $numbers[2]\n";
print "The sum is $sum\n";
print "The product is $prod\n";
my $max = $numbers[0]; # Assign the first number as $max
foreach my $number (1..$#numbers) { # $#numbers allows for more than three
if ( $numbers[$number] > $max ) { # Check other numbers against $max
$max = $numbers[$number]; # Assign to $max if number is bigger than current $max
}
}
print "The largest number is $max\n";
print "Since they're sorted, the largest number is $numbers[-1]\n";
i know how to use max.. i want to do it with IF THEN ELSE
Well, this would work, but it's an awfully silly way to find the largest of three numbers. (Try imagining this sort of thing for 40 or 50 numbers.)
Code:
if ( $numbers[0] > $numbers[1] ) { # Check if the first number is bigger than the second
if ( $numbers[0] > $numbers[2] ) { # It is, so check first against third
print "$numbers[0] is larger than $numbers[1] or $numbers[2]\n"; # First is biggest
}
else {
print "$numbers[2] is larger than $numbers[0] or $numbers[1]\n"; # Failed last test, so third is biggest
}
}
else { # First wasn't bigger than second, so move onto other tests
if ( $numbers[1] > $numbers[2] ) { # Check second against third
print "$numbers[1] is larger than $numbers[0] or $numbers[2]\n"; # Second is biggest
}
else {
print "$numbers[2] is larger than $numbers[0] or $numbers[1]\n"; # Third is biggest
}
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.