Re: items in array

[email protected] ("Dr.Ruud")
Newsgroups perl.beginners
Organization Chaos rules.
Message-ID <[email protected]>
"Li, Jialin" schreef:
> On Wed, May 14, 2008 at 4:01 PM, Tech list
> <[email protected]> wrote:
>
>> what is the correct way to get the number of items in an array?
>>
>> I used to use $#array
>>
>>
>> --
>> To unsubscribe, e-mail: [email protected]
>> For additional commands, e-mail: [email protected]
>> http://learn.perl.org/
>>
>>
>>
>
> $#array is the index of the last item, so the number should be
> $#array + 1, or
> scalar @array

Please read perlvar about $[. Normally it is 0, but you can set it to a
different value, like -3 or 27.

The $#array is the last index of @array, so $array[$#array] is
equivalent to $array[-1].

The number of items in an array is returned by @array in scalar context,
examples:

    scalar(@array)

    (0+ @array)

    if (0 < @array) { ... }

    if (@array) { ... }

-- 
Affijn, Ruud

"Gewoon is een tijger."
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.