Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

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

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 (Monsignor) 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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://955909]
[Corion]: Sane Dots Supplies - your brand manufacturer for satisfying experiences!
[choroba]: preparing the trip to Vietnam + buying a new car = enough fun
[Corion]: choroba: Yeah, that's a lot of "fun" for one week...
[Corion]: I guess I should stage some product photos for Sane Dots Supplies ;)
[marto]: I had some ideas about that Corion
[marto]: I'll send a message over the weekend maybe :P
[Corion]: choroba: I avoided the "fun" of emergency shopping for a new flat iron by trying the flat iron without the timer that sat between it and the mains power. Seems as if just the timer is broken.
[Corion]: marto: Heh, would be cool!

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2017-01-20 10:19 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (174 votes). Check out past polls.