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

Re: size of an array ref

by swampyankee (Parson)
on Feb 24, 2012 at 12:58 UTC ( #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


Comment on Re: size of an array ref
Download Code
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 (Prior) 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
Node Status?
node history
Node Type: note [id://955909]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (9)
As of 2015-07-03 15:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (53 votes), past polls