Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Unexpected Output

by k_manimuthu (Monk)
on Jul 28, 2010 at 07:57 UTC ( #851677=perlquestion: print w/ replies, xml ) Need Help??
k_manimuthu has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I need to assign $string = 'some values' based on the length of the array. If length of the array equals to '-1' I want to assign $string = 'True', otherwise I expect $string = 'False'.

I got $string = 'False' even though the array values is equals to '-1'. But i am add a line to test the array value, I got the output as 'True'. I am confused, why the code's gives the mismatch output.

Below I placed the code. Could someone please explain on this.

Mani Muthu

use strict; use warnings; my @arr; my $string = 'Initial'; ($#arr == -1 ) ? print "True" : print "False"; ($#arr == -1 ) ? $string = 'True' : $string = 'False'; print "\n\$string = $string"; print "\n\$#arr = $#arr"; =Output True $string = False $#arr = -1 =cut

Note : I am using perl 5.8.9 in Windows

Comment on Unexpected Output
Download Code
Re: Unexpected Output
by moritz (Cardinal) on Jul 28, 2010 at 08:15 UTC
    The problem is that the assignment operator has looser precedence than the ternary/conditional operator ? :.

    Instead, use

    $string = $#arr == -1 ? 'True' : 'False';

    If you want to assign to two different variables, use an ordinary if construct instead of the ternary operator.

    Perl 6 - links to (nearly) everything that is Perl 6.
Re: Unexpected Output
by Corion (Pope) on Jul 28, 2010 at 08:17 UTC

    If you only want to check whether an array is empty, it's nicer to use

    if (@arr) { ... };
Re: Unexpected Output
by cdarke (Prior) on Jul 28, 2010 at 08:22 UTC
    This is a precedence problem with your ternary statement. If you parenthesise:
    ($#arr == -1 ) ? ($string = 'True' ): ($string = 'False');
    then the output is as expected. However it is probably better written as:
    $string = ($#arr == -1 ) ? 'True' : 'False';

    By the way, $#arr is not the length of the array, it is the highest index number. So your ternary could be written like this:
    $string = @arr ? 'False' : 'True';
    The array in scalar context gives the number of elements (zero is false).
Re: Unexpected Output
by Khen1950fx (Canon) on Jul 28, 2010 at 08:33 UTC
    I added some parentheses...Still a little messy, but it works.
    #!/usr/bin/perl use strict; use warnings; my @arr; my $string = 'Initial'; ($#arr == -1 ) ? print('True') : print ('False'); ($#arr == -1 ) ? ($string = 'True') : ($string = 'False'); print "\n\$string = $string"; print "\n\$#arr = $#arr\n";
Re: Unexpected Output
by nvivek (Vicar) on Jul 28, 2010 at 11:41 UTC

    For checking the length of an array,don't use $#arrayname because it won't give the number of elements in an array and it gives the highest index of the array which is number of elements - 1 in the array.If you want to check the length of an array, you assign an array to scalar, then it will give the actual number of elements in the array.There is one more problem in $#arrayname, it gives the value by adding the value of $[ with highest index of an array.In your script, if you change the value of $[ variable, then $#arrayname won't give the correct value.

Re: Unexpected Output
by Your Mother (Canon) on Jul 28, 2010 at 16:21 UTC

    Please use the $string = @arr ? 'False' : 'True'; idiom cdarke offered. $#arr is (so I say) a poor style choice to use anywhere (in general) and both confusing and potentially error prone here (as nvivek notes).

      Dear All - Thanks a lot for your suggestion and solutions.
      Thanks to introduce some new things to me. Specially for ...

      $string = ($#arr == -1 ) ? 'True' : 'False';

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://851677]
Approved by Corion
Front-paged by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2014-07-26 01:28 GMT
Find Nodes?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:

    Results (175 votes), past polls