Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

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

by usless_blonde (Initiate)
on Oct 14, 2003 at 12:53 UTC ( [id://299087]=note: print w/replies, xml ) Need Help??


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

Just to be reaaly stupid. What does $#array mean?
I can't do that as everything after the # is ignored!

{QandAEditor note-- Summarizing a followup to this node: $#array is the index of the last element in @array. #, in this case, doesn't begin a comment.

$#array is not necessarily equal to scalar @array - 1. Use @array in scalar context to determine the size of an array. Use $#array when determining the index number of the last element of @array. }

Replies are listed 'Best First'.
Re: Answer: How do I find the size of an array?
by DrHyde (Prior) on Oct 14, 2003 at 15:23 UTC
    $#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://299087]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (7)
As of 2024-04-24 21:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found