Counting array elements in Perl

前端 未结 6 1553
逝去的感伤
逝去的感伤 2021-02-02 09:29

How do I get the total items in an array, NOT the last id?

None of two ways I found to do this works:

my @a;
# Add some elements (no consecutive ids)
$a[         


        
6条回答
  •  醉酒成梦
    2021-02-02 09:47

    @people = qw( bob john linda ); 
    $n = @people; # the number 3
    Print " le number in the list is $n \n"; 
    

    Expressions in Perl always return the appropriate value for their context. For example, how about the “name” * of an array. In a list context, it gives the list of elements. But in a scalar context, it returns the number of elements in the array:

提交回复
热议问题