Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

How can I change the perl program involving if-elsif-else conditional to get correct result?

by supriyoch_2008 (Scribe)
on Apr 23, 2012 at 07:28 UTC ( #966532=perlquestion: print w/ replies, xml ) Need Help??
supriyoch_2008 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perlmonks,

I am a beginner in perl programming. I have used if-elsif-else conditional statement in my program to find the actual result. But I am getting errors in running the perl code. Can I get help from perl monks to correct the code so that I get correct result? Here, I know the values of $a, $b and $c and that $a is largest. But later on,I want to work with scalar variables whose values will be determined in the perl program. If I get correct result from this program, I shall try to set it in my bigger program. My objective is to find out which scalar variable out of three is the largest variable and then to do further calculations in each block as shown.

#!usr/bin/perl-w $a=0.9; $b=0.7; $c=0.5; if($a>$b>$c){print"\n$a is largest.\n";$x1=$b/$a;$x2=$c/$a;# Line 3 $x1r=sprintf("%.2f",$x1);$x2r=sprintf("%.2f",$x2); print"\n values are $x1r, $x2r"; } elsif($b>$c>$a){print"\n$b is largest.\n";$x3=$c/$b;$x2=$a/$b;# Line + 6 $x3r=sprintf("%.2f",$x3);$x4r=sprintf("%.2f",$x4); print"\n values are $x3r, $x4r"; } else($c>$a>$b){print"\n$c is largest.\n";$x5=$c/$b;$x6=$a/$b;# Line +9 $x5r=sprintf("%.2f",$x5);$x6r=sprintf("%.2f",$x6); print"\n values are $x5r, $x6r"; } exit;# Line 13

RESULTS Obtained:

Microsoft Windows [Version 6.1.7600] Copyright (c) 2009 Microsoft Corporation. All rights reserved. C:\Users\DR-SUPRIYO>cd desktop C:\Users\DR-SUPRIYO\Desktop>test.pl syntax error at C:\Users\DR-SUPRIYO\Desktop\test.pl line 3, near "$b>" syntax error at C:\Users\DR-SUPRIYO\Desktop\test.pl line 6, near "}" syntax error at C:\Users\DR-SUPRIYO\Desktop\test.pl line 9, near "}" syntax error at C:\Users\DR-SUPRIYO\Desktop\test.pl line 12, near "}" Execution of C:\Users\DR-SUPRIYO\Desktop\test.pl aborted due to compil +ation erro rs.
My Expected Result should look like this: 0.9 is largest. values are 0.78, 0.56

Comment on How can I change the perl program involving if-elsif-else conditional to get correct result?
Select or Download Code
Re: How can I change the perl program involving if-elsif-else conditional to get correct result?
by nemesdani (Friar) on Apr 23, 2012 at 07:47 UTC
    if($a>$b>$c) is not the way you should think, this is erronous.

    Write if($a>$b and $b>$c) instead, and correct all the other conditions as well. You should also consider filling a list with these values and sort them instead of this tiresome if-elsif-else method.

    I'm too lazy to be proud of being impatient.

      if($a>$b>$c) is not the way you should think, this is erronous
      Though it may be "erronous" in Perl 5, I don't think it is erroneous in general. In fact, I think it is a sweet and natural syntax, aka "Chained Comparisons", and supported by both Perl 6 and Python.

        I omitted the "if you'd like to code correctly in Perl 5", yes, but I assumed that was the goal of the OP. :P "

        About chained comparisons: It's more natural than the current common practice, yes, but personally I don't find it nice. I never wrote a compiler, nor do I plan to, but I just don't think it's worth the trouble to implement it.

        Thank God, other people think otherwise, so in 2029 (my daughter graduates, Perl6 comes out) I'll be happy to get used to chained comparisons.

        I'm too lazy to be proud of being impatient.
Re: How can I change the perl program involving if-elsif-else conditional to get correct result?
by rovf (Priest) on Apr 23, 2012 at 08:41 UTC
    Have a look at perlop, in particular at the section Operator Precedence and Associativity. It says that the comparision operators are non-associative.


    May I ask you what programming language you had used before? Or is Perl your first one?

    -- 
    Ronald Fischer <ynnor@mm.st>
Re: How can I change the perl program involving if-elsif-else conditional to get correct result?
by 2teez (Priest) on Apr 23, 2012 at 11:03 UTC

    You can do the following, but I will also advise you understand how perl works

    Put all your variable into a subroutine, and then iterate over them getting and returning the largest as done below in case (a)

    Case a:

    #!/usr/bin/perl use warnings; use strict; my $a=0.9; my $b=0.7; my $c=0.5; my $lar_value=max($a,$b,$c); print $lar_value," is the greatest",$/; sub max{ my $init=shift(@_); foreach(@_){ if($init>$_){ $init=$init; }else{$init= $_} } return $init; }

    Case b: One can also use a module **List::Util** from CPAN http://search.cpan.org/~pevans/Scalar-List-Utils-1.25/lib/List/Util.pm. like this

    #!/usr/bin/perl use warnings; use strict; use List::Util qw(max); my $a=0.9; my $b=0.7; my $c=0.5; my $lar_value=max($a,$b,$c); print $lar_value, " is the greatest\n";
    I hope this helps.
      Or instead of reinventing the wheel, you could sort a list:

      my @list = sort ($a, $b, $c); print $list[$#list];

      I'm too lazy to be proud of being impatient.
        If you are going to do it like that, you need a numeric sort, not the default alphabetic sort:
        @sorted = sort{$a <=> $b}($x, $y, $z); print $sorted[-1];
        Also, $a and $b are special variables reserved for sort and other purposes. Don't uses these for normal user variables.
Re: How can I change the perl program involving if-elsif-else conditional to get correct result?
by BillKSmith (Chaplain) on Apr 23, 2012 at 12:44 UTC
    The module List::Util is probably included in your perl distribution.
    use List::Util qw( max ); . . . print 'Largest: ', max( $a, $b, $c );
Re: How can I change the perl program involving if-elsif-else conditional to get correct result?
by brx (Pilgrim) on Apr 23, 2012 at 13:40 UTC
    Some fun with conditional operator (perlop).
    #!/usr/bin/perl -w use strict; my $xa=0.9; my $xb=0.7; my $xc=0.5; my @triplet = ($xa,$xb,$xc); my $m = ( ($xa>$xb and $xa>$xc)? 0: ($xb>$xc? 1: 2) ); print "$triplet[$m] is largest\nvalues are ", sprintf("%.2f, %.2f", $triplet[($m+1)%3]/$triplet[$m], $triplet[($m+2)%3]/$triplet[$m]);
    Update: typo in code
    Update2: better perf
    my $m = ( $xa>$xb? ($xa>$xc?0:2): ($xb>$xc?1:2) );

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://966532]
Approved by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (10)
As of 2014-12-18 12:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (51 votes), past polls