Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re: how internally $#array is working in Perl

by sflitman (Hermit)
on Aug 13, 2010 at 05:44 UTC ( #854833=note: print w/replies, xml ) Need Help??

in reply to how internally $#array is working in Perl

My understanding is that $#x is the n-1 index for a 0-based array. All Perl arrays are 0-based, until you change $[. Why do you wish to change $[? Also, it may be safer to use scalar(@x) for how many elements there are in x. There are ominous warnings about $# going away at some point in several of the perldocs.

As for the actual internals, you would need to look at the Perl source. (shiver) --> perlguts


  • Comment on Re: how internally $#array is working in Perl

Replies are listed 'Best First'.
Re^2: how internally $#array is working in Perl
by DrHyde (Prior) on Aug 13, 2010 at 10:02 UTC

    $#array is the index of the last element in the array. Perl obviously knows how many elements there are in an array - if it didn't, then it wouldn't be able to automagically grow the array as you add more stuff to it and you'd have to handle memory allocation yourself. And perl obviously knows the index of the first element, which is the value in $[. Knowing how big the array is and the starting index, calculating the last index is trivial.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://854833]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2018-02-20 06:09 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (267 votes). Check out past polls.