LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-13-2008, 05:32 PM   #1
b3n
LQ Newbie
 
Registered: Apr 2007
Posts: 20

Rep: Reputation: 0
Perl IfThenElse problem


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
 
Old 12-13-2008, 06:00 PM   #2
Su-Shee
Member
 
Registered: Sep 2007
Location: Berlin
Distribution: Slackware
Posts: 510

Rep: Reputation: 53
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.
 
Old 12-13-2008, 06:07 PM   #3
b3n
LQ Newbie
 
Registered: Apr 2007
Posts: 20

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Su-Shee View Post
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
is that what you mean?
 
Old 12-13-2008, 06:15 PM   #4
Su-Shee
Member
 
Registered: Sep 2007
Location: Berlin
Distribution: Slackware
Posts: 510

Rep: Reputation: 53
No.

I mean:

Code:
if ($numbers[0] > $numbers[1]) {
    print "Largest Number is $numbers[0]";
} elseif ($numbers[1] < $numbers[0]) {
    print "Largest Number is $numbers[1]";
} else {
    whatever;
}
Please read perldoc perlsyn carefully.
 
Old 12-13-2008, 06:46 PM   #5
b3n
LQ Newbie
 
Registered: Apr 2007
Posts: 20

Original Poster
Rep: Reputation: 0
Code:
#!/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.


this is just bad luck!..

Last edited by b3n; 12-13-2008 at 06:55 PM.
 
Old 12-13-2008, 07:59 PM   #6
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
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.
 
Old 12-13-2008, 08:23 PM   #7
b3n
LQ Newbie
 
Registered: Apr 2007
Posts: 20

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Telemachos View Post
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
 
Old 12-13-2008, 09:00 PM   #8
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
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
  }
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
need perl help...perl module problem john83reuben Programming 2 03-22-2008 09:08 PM
Problem with Perl azeiss Linux - Newbie 2 04-02-2007 02:57 AM
Problem with perl module for w3c validator to work on my local Apache+PHP+perl instal tbamt Linux - Software 0 12-16-2004 05:37 PM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 08:09 PM
perl problem? apache problem? cgi problem? WorldBuilder Linux - Software 1 09-17-2003 07:45 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:49 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration