Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Get the length of a list.

by nbtrap (Sexton)
on Jul 27, 2013 at 15:09 UTC ( #1046656=perlquestion: print w/ replies, xml ) Need Help??
nbtrap has asked for the wisdom of the Perl Monks concerning the following question:

Evalutating a list in scalar context returns the last element in the list, not its length:
print scalar(1, 2, 3, 4, 3, 2, 1);
will print "1". What's the best way to print the length of a list? The only way I can think of is:
print scalar @{ [1, 2, 3, 4, 3, 2, 1] };
But that feels like a hack, and I wonder if there's a better way.

Comment on Get the length of a list.
Select or Download Code
Re: Get the length of a list.
by Anonymous Monk on Jul 27, 2013 at 15:18 UTC
    Assuming that you have:
    @arr=(1, 2, 3, 4, 3, 2, 1);
    you can do:
    $length_of_array=@arr; print $length_of_array."\n";
Re: Get the length of a list.
by LanX (Canon) on Jul 27, 2013 at 15:36 UTC
Re: Get the length of a list.
by mtmcc (Hermit) on Jul 27, 2013 at 16:07 UTC
    Also, you can find the last index in the array like this:

    my @list = (1, 2, 3, 4, 3, 2, 1); print STDERR "Last index: $#list\n";

    It's clearly not the length, but it is handy in some settings.
Re: Get the length of a list.
by Anonymous Monk on Jul 27, 2013 at 16:09 UTC

    if all you want is the length of an array, why not?

    print $#;
      Lists are not arrays!

      see FAQ#What is the difference between a list and an array?

      for instance the easiest way to know how many values a function or a grep returned is using a goatse:

      DB<101> sub tst {a..z} DB<102> $length =()= tst() => 26 DB<107> grep { $_ % 2 } 1..10 => (1, 3, 5, 7, 9) DB<108> $n =()= grep { $_ % 2 } 1..10 => 5

      Cheers Rolf

      ( addicted to the Perl Programming Language)

Re: Get the length of a list.
by farang (Hermit) on Jul 27, 2013 at 16:35 UTC

    Another way, though not necessarily better than the original.

    print scalar map $_, (1, 2, 3, 4, 3, 2, 1);
    But if other processing is going to be done with the list, might as well just make a named array out of it from the start.

Re: Get the length of a list.
by zork42 (Monk) on Jul 28, 2013 at 04:44 UTC
    Evalutating a list in scalar context returns the last element in the list, not its length:

    print scalar(1, 2, 3, 4, 3, 2, 1);

    will print "1".
    Nope, evaluating a list in scalar context returns the length of the list.
    (*) Apologies, I was getting lists and arrays confused there. I was talking about arrays, not lists. That should have read:
    "Evaluating an array in scalar context returns the length of the array."
    However, I hope my following explaination of why scalar(list) does not return the length of the list is still useful.


    The documentation for scalar says:
    Because scalar is a unary operator, if you accidentally use a parenthesized list for the EXPR, this behaves as a scalar comma expression, evaluating all but the last element in void context and returning the final element evaluated in scalar context. This is seldom what you want.
    So what is actually happening here is (1, 2, 3, 4, 3, 2, 1) evaluates to 1, the last element in the list.
    And scalar 1 means evaluate 1 in scalar context, which is *drum roll* 1 ! :)

    use warnings; would have detected this.

    Example code:
    use strict; use warnings; print "A: ", scalar(10, 20, 30, 40, 50, 60, 70), "\n"; # = scalar +(70) = scalar 70 = 70 print "B: ", scalar(70), "\n"; # + = scalar 70 = 70 print "C: ", scalar 70, "\n"; # + = 70 my $x = scalar(10, 20, 30, 40, 50, 60, 70); # = scalar +(70) = scalar 70 = 70 print "D: ", $x, "\n"; $x = (10, 20, 30, 40, 50, 60, 70); # = +(70) = 70 print "E: ", $x, "\n"; my @a = (10, 20, 30, 40, 50, 60, 70); print "F: ", scalar @a, "\n"; # = 7, the + length of @a
    Output:
    Useless use of a constant (10) in void context at D:\stuart\play\temp\ +pp.pl line 4. Useless use of a constant (20) in void context at D:\stuart\play\temp\ +pp.pl line 4. Useless use of a constant (30) in void context at D:\stuart\play\temp\ +pp.pl line 4. Useless use of a constant (40) in void context at D:\stuart\play\temp\ +pp.pl line 4. Useless use of a constant (50) in void context at D:\stuart\play\temp\ +pp.pl line 4. Useless use of a constant (60) in void context at D:\stuart\play\temp\ +pp.pl line 4. Useless use of a constant (10) in void context at D:\stuart\play\temp\ +pp.pl line 10. Useless use of a constant (20) in void context at D:\stuart\play\temp\ +pp.pl line 10. Useless use of a constant (30) in void context at D:\stuart\play\temp\ +pp.pl line 10. Useless use of a constant (40) in void context at D:\stuart\play\temp\ +pp.pl line 10. Useless use of a constant (50) in void context at D:\stuart\play\temp\ +pp.pl line 10. Useless use of a constant (60) in void context at D:\stuart\play\temp\ +pp.pl line 10. Useless use of a constant (10) in void context at D:\stuart\play\temp\ +pp.pl line 13. Useless use of a constant (20) in void context at D:\stuart\play\temp\ +pp.pl line 13. Useless use of a constant (30) in void context at D:\stuart\play\temp\ +pp.pl line 13. Useless use of a constant (40) in void context at D:\stuart\play\temp\ +pp.pl line 13. Useless use of a constant (50) in void context at D:\stuart\play\temp\ +pp.pl line 13. Useless use of a constant (60) in void context at D:\stuart\play\temp\ +pp.pl line 13. A: 70 B: 70 C: 70 D: 70 E: 70 F: 7


    UPDATE: (a few minutes later) added comments to code, and 1 extra example

    UPDATE #2: As choroba pointed out in the following post, I was getting lists and arrays confused. See (*) above.
      Nope, evaluating a list in scalar context returns the length of the list.

      You seem to confuse lists and arrays.

      لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
        You are correct choroba.
        Thanks for pointing out my error :)


        perldata - Context highlights the difference between scalar(array) and scalar(list):
        If you evaluate an array in scalar context, it returns the length of the array.
        (Note that this is not true of lists, which return the last value, like the C comma operator,
        nor of built-in functions, which return whatever they feel like returning.)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2014-07-22 07:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (106 votes), past polls