2011-09-13 18:40
采纳率: 25%
浏览 187

用 Perl 查找数组的大小

I seem to have come across several different ways to find the size of an array. What is the difference between these three methods?

my @arr = (2);
print scalar @arr; # First way to print array size

print $#arr; # Second way to print array size

my $arrSize = @arr;
print $arrSize; # Third way to print array size


  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

11条回答 默认 最新

  • csdnceshi52
    妄徒之命 2011-09-13 18:44

    The first and third ways are the same: they evaluate an array in scalar context. I would consider this to be the standard way to get an array's size.

    The second way actually returns the last index of the array, which is not (usually) the same as the array size.

    点赞 评论
  • weixin_41568183
    零零乙 2011-09-13 18:44

    This gets the size by forcing the array into a scalar context, in which it is evaluated as its size:

    print scalar @arr;

    This is another way of forcing the array into a scalar context, since it's being assigned to a scalar variable:

    my $arrSize = @arr;

    This gets the index of the last element in the array, so it's actually the size minus 1 (assuming indexes start at 0, which is adjustable in Perl although doing so is usually a bad idea):

    print $#arr;

    This last one isn't really good to use for getting the array size. It would be useful if you just want to get the last element of the array:

    my $lastElement = $arr[$#arr];

    Also, as you can see here on Stack Overflow, this construct isn't handled correctly by most syntax highlighters...

    点赞 评论
  • csdnceshi66

    First, the second is not equivalent to the other two. $#array returns the last index of the array, which is one less than the size of the array.

    The other two are virtually the same. You are simply using two different means to create scalar context. It comes down to a question of readability.

    I personally prefer the following:

    say 0+@array;          # Represent @array as a number

    I find it clearer than

    say scalar(@array);    # Represent @array as a scalar


    my $size = @array;
    say $size;

    The latter looks quite clear alone like this, but I find that the extra line takes away from clarity when part of other code. It's useful for teaching what @array does in scalar context, and maybe if you want to use $size more than once.

    点赞 评论
  • csdnceshi69
    YaoRaoLov 2013-06-12 07:48


    my @a = (undef, undef);
    my $size = @a;
    warn "Size: " . $#a;   # Size: 1. It's not the size
    warn "Size: " . $size; # Size: 2
    点赞 评论
  • csdnceshi50
    三生石@ 2013-09-14 03:29

    To use the second way, add 1:

    print $#arr + 1; # Second way to print array size
    点赞 评论
  • weixin_41568174
    from.. 2013-10-23 04:32

    All three give the same result if we modify the second one a bit:

    my @arr = (2, 4, 8, 10);
    print "First result:\n";
    print scalar @arr; 
    print "\n\nSecond result:\n";
    print $#arr + 1; # Shift numeration with +1 as it shows last index that starts with 0.
    print "\n\nThird result:\n";
    my $arrSize = @arr;
    print $arrSize;
    点赞 评论
  • csdnceshi63
    elliott.david 2015-08-03 15:48

    The “Perl variable types” section of the perlintro documentation contains

    The special variable $#array tells you the index of the last element of an array:

    print $mixed[$#mixed];       # last element, prints 1.23

    You might be tempted to use $#array + 1 to tell you how many items there are in an array. Don’t bother. As it happens, using @array where Perl expects to find a scalar value (“in scalar context”) will give you the number of elements in the array:

    if (@animals < 5) { ... }

    The perldata documentation also covers this in the “Scalar values” section.

    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.) The following is always true:

    scalar(@whatever) == $#whatever + 1;

    Some programmers choose to use an explicit conversion so as to leave nothing to doubt:

    $element_count = scalar(@whatever);

    Earlier in the same section documents how to obtain the index of the last element of an array.

    The length of an array is a scalar value. You may find the length of array @days by evaluating $#days, as in csh. However, this isn’t the length of the array; it’s the subscript of the last element, which is a different value since there is ordinarily a 0th element.

    点赞 评论
  • weixin_41568110
    七度&光 2017-03-30 09:40

    To find the size of an array use the scalar keyword:

    print scalar @array;

    To find out the last index of an array there is $# (Perl default variable). It gives the last index of an array. As an array starts from 0, we get the size of array by adding one to $#:

    print "$#array+1";


    my @a = qw(1 3 5);
    print scalar @a, "\n";
    print $#a+1, "\n";


    点赞 评论
  • csdnceshi72
    谁还没个明天 2017-05-06 21:40

    From perldoc perldata, which should be safe to quote:

    The following is always true:

    scalar(@whatever) == $#whatever + 1;

    Just so long as you don't $#whatever++ and mysteriously increase the size or your array.

    The array indices start with 0.


    You can truncate an array down to nothing by assigning the null list () to it. The following are equivalent:

        @whatever = ();
        $#whatever = -1;

    Which brings me to what I was looking for which is how to detect the array is empty. I found it if $#empty == -1;

    点赞 评论
  • weixin_41568174
    from.. 2017-05-09 15:12

    There are various ways to print size of array. Here are the meanings of all: Lets say our array is my @arr = (3,4);

    Method 1: scalar

    This is the right way to get size of arrays.

    print scalar @arr;  # prints size, here 2

    Method 2: Index number

    $#arr gives the last index of an array. so if array is of size 10 then its last index would be 9.

    print $#arr;     # prints 1, as last index is 1
    print $#arr + 1; # Add 1 to last index to get array size

    We are adding 1 here considering array as 0-indexed. But, if its not zero based then, this logic will fail.

    perl -le 'local $[ = 4; my @arr=(3,4); print $#arr + 1;'   # prints 6

    Above example prints 6, because we have set its initial index to 4. Now index would be 5 and 6, with elements 3 and 4 respectively.

    Method 3:

    When an array is used in scalar context, then it returns the size of array

    my $size = @arr;
    print $size;   # prints size, here 2

    Actually method 3 and method 1 are same.

    点赞 评论
  • csdnceshi64
    游.程 2018-03-13 20:42

    What about int(@array) as it threats the argument as scalar.

    点赞 评论