Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

size of array from ref

by nop (Hermit)
on Apr 01, 2002 at 19:52 UTC ( [id://155835]=perlquestion: print w/replies, xml ) Need Help??

nop has asked for the wisdom of the Perl Monks concerning the following question:

If I have a ref to an array, how can find its size efficiently? I suspect  (scalar @{$aref}) actually builds the array again, just to size it. The array is large, so this is slow.....
thanks
nop

Replies are listed 'Best First'.
Re: size of array from ref
by VSarkiss (Monsignor) on Apr 01, 2002 at 20:06 UTC

    Would Larry do something that stupid? ;-)

    The code you have will work fine, without creating a duplicate array. You can also get the highest index in the array with $#$aref, which, if you haven't changed the default least index from 0 (by setting $[) will be one less than the number of elements in the array.

Re: size of array from ref
by Fletch (Bishop) on Apr 01, 2002 at 20:01 UTC

    @{$foo} no more builds a new array than $foo->[5] builds one get the 5th element.

Re: size of array from ref
by rinceWind (Monsignor) on Apr 01, 2002 at 20:01 UTC
    I think that $#$aref does the trick - you might need to add one to give the number of elements.
Re: size of array from ref
by Moonie (Friar) on Apr 01, 2002 at 20:00 UTC
Re: size of array from ref
by Juerd (Abbot) on Apr 01, 2002 at 20:43 UTC

    I suspect (scalar @{$aref}) actually builds the array again, just to size it. The array is large, so this is slow.....

    Well, no. scalar @$array_reference will do what you want it to do. Dereferencing is not copying, you're just re-shaping it temporarily, so it can be used as if it were a real array (surprise: with @$ref, you HAVE a real array in every way. It's not a copy, it's the array $ref is referencing to).

    The reference doesn't hold the actual array, it merely refers (points) to it. The array itself is somewhere else in memory, and may or may not have a normal @foo name, or maybe multiple. By dereferencing, you tell Perl to use the array instead of the reference.

    U28geW91IGNhbiBhbGwgcm90MTMgY
    W5kIHBhY2soKS4gQnV0IGRvIHlvdS
    ByZWNvZ25pc2UgQmFzZTY0IHdoZW4
    geW91IHNlZSBpdD8gIC0tIEp1ZXJk
    

Re: size of array from ref
by tradez (Pilgrim) on Apr 01, 2002 at 20:57 UTC
    Just use
    $size = $#$aref + 1;
    Quick, simply, concise, just the way we monks like it.

    Tradez
    "Never underestimate the predicability of stupidity"
    - Bullet Tooth Tony, Snatch (2001)
Re: size of array from ref
by Pearte (Beadle) on Apr 02, 2002 at 00:34 UTC
    $size = @array; #Gives size of array.

    Can be used as follows ...
    for($i=0;$i<$size;$i++) { #Do work here; }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-25 17:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found