good chemistry is complicated,and a little bit messy -LW PerlMonks

### size of an array ref

by fionbarr (Friar)
 on Feb 24, 2012 at 12:46 UTC Need Help??

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

How do I know the size of a reference to an array....
```  DB<22> x @a
0  ARRAY(0x8d1814)
0  123
1  456
2  789
if I say scalar @a....I get '1'...I want to get '3'. Thanks

Replies are listed 'Best First'.
Re: size of an array ref
by roboticus (Chancellor) on Feb 24, 2012 at 13:03 UTC

You need to use scalar(@{\$a[0]}) to get 3. Your array contains only one element, the array reference. So if you want 3, you need to ask the referenced array for its size, not the container. Perhaps this example will help:

```\$ cat t.pl
#/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;

my @a = (1, 2, 3);

print "Size of a: ", scalar(@a), "\n";
print Dumper(\@a),"\n";

my @b = [0, 1, 2];
print "Size of b: ", scalar(@b), "\n";
print Dumper(\@b),"\n";

print "Size of b[0]: ", scalar(@{\$b[0]}), "\n";
print Dumper(\$b[0]),"\n";

which gives us:

```\$ perl t.pl
Size of a: 3
\$VAR1 = [
1,
2,
3
];

Size of b: 1
\$VAR1 = [
[
0,
1,
2
]
];

Size of b[0]: 3
\$VAR1 = [
0,
1,
2
];

...roboticus

When your only tool is a hammer, all problems look like your thumb.

that's it! Thanks guys! # array: \$ perl -wE 'my @a = qw/a b c/; say scalar @a' 3 # array ref: \$ perl -wE 'my \$a = qw/a b c/; say scalar @\$a'perfect!
Re: size of an array ref
by moritz (Cardinal) on Feb 24, 2012 at 12:52 UTC
```# array:
\$ perl -wE 'my @a = qw/a b c/; say scalar @a'
3
# array ref:
\$ perl -wE 'my \$a = [qw/a b c/]; say scalar @\$a'
3

that makes sense...I might have a reference to a reference to an array...I've tried a lot of permutations and not gotten '3'...is it possible to get the size of a ref to a ref?
```      @array;
@\$arrayref;
@\$\$arrayrefref;
@\$\$\$arrayrefrefref;
@\$\$\$\$arrayrefrefrefref;
@\$\$\$\$\$arrayrefrefrefrefref;
@\$\$\$\$\$\$arrayrefrefrefrefrefref;
... etc ...
```\$a= [1..10];\$b=\\$a;\$c=\\$b; \$d=\\$c;\$e=\\$d;

\$e=\$\$e while (ref(\$e) eq "REF");
print scalar @\$e;
Re: size of an array ref
by swampyankee (Parson) on Feb 24, 2012 at 12:58 UTC

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

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

```#!/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.
Re: size of an array ref
by Anonymous Monk on Feb 24, 2012 at 19:02 UTC
Re: size of an array ref
by TomDLux (Vicar) on Feb 27, 2012 at 19:49 UTC

@a is an array. The first element of @a is \$a[0], which stores a reference to an array of three elements. You can extract the inner array using @{ \$a[0] }. Unlike the other suggestions, this works for the (fictional) array stored in \$a[1] or other positions, and it can be extended for the third-level, fourth-level, etc.

As Occam said: Entia non sunt multiplicanda praeter necessitatem.

Create A New User
Node Status?
node history
Node Type: perlquestion [id://955907]
Approved by marto
Front-paged by SuicideJunkie
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (3)
As of 2020-10-24 21:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
My favourite web site is:

Results (247 votes). Check out past polls.

Notices?