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

Re: Answer: How do I find the size of an array?

by DrHyde (Prior)
on Oct 14, 2003 at 15:23 UTC ( [id://299143]=note: print w/replies, xml ) Need Help??


in reply to Re: How do I find the size of an array?
in thread How do I find the size of an array?

$#array is the index of the last element in @array. In this case, the # character does NOT introduce a comment.

For example, if you have @array = qw(ant bat cat dog eagle fish ghoti) then $#array is 6 (the index of 'ghoti'). The number of elements in the array is usually $#array + 1.

Note that those values will change if $[ is set to anything other than 0. To cope with those times when someone has been naughty (use of $[ is strongly discouraged), you should get the number of elements in an array by evaluating @array in scalar context, such as:

print scalar @array;
or
$num_elements = @array;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://299143]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2025-06-25 04:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.