Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: size of an array ref

by swampyankee (Parson)
on Feb 24, 2012 at 12:58 UTC ( [id://955909]=note: print w/replies, xml ) Need Help??


in reply to size of an array ref

A question I can still answer!

The size of a reference to an array is a scalar. If you're looking for the size of the array to which the references is pointing, you may try this:

print scalar(@{a});

The curly braces are the dereference operator. Now, if I misunderstood, and you want to know something like the total size of a array of references to arrays, my answer is likely useless.


Information about American English usage here and here. Floating point issues? Please read this before posting. — emc

Replies are listed 'Best First'.
Re^2: size of an array ref
by jethro (Monsignor) on Feb 24, 2012 at 16:49 UTC
    Sorry to disappoint you but the curly braces do not dereference, @{a} is equivalent to @a. The curly braces only allow more complicated constructs with unambiguous precedence, for example ${$a[3]}[4] versus ${$a}[3][4]. If you want dereference, @{$a} or simply @$a is the right choice.

    Update: Thanks to choroba and SuicideJunkie for spotting a $ where a @ should have been

      Insert random cursing here. I should have kept my virtual mouth shut.


      Information about American English usage here and here. Floating point issues? Please read this before posting. — emc

Re^2: size of an array ref
by Marshall (Canon) on Feb 26, 2012 at 01:38 UTC
    #!/usr/bin/perl -w use strict; my $aref = [5,6,7,8]; print @$aref,"\n"; #5678 print "@$aref","\n"; #5 6 7 8 print scalar @$aref,"\n"; #4 print ''.@$aref,"\n"; #4 # using string concatenation in the last example # puts @$aref in a scalar context my $ref = [ [qw(a b c)], [qw(x y zzy)] ]; print ''.map{@$_}@$ref; #6 # $ref is a ref to an array of references # @$ref makes a list of those references # the map expands out each array ref into all of its elements # putting this into a scalar context reports the total number print @{$ref->[1]},"\n"; #xyzzy #basically, you need extra {} when a subscript is involved.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-03-29 09:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found