size of anonymous array

J

jtbutler78

I have a hash with the values being arrays. How can I find the size of
these arrays?

example:
$hash{$key} = [ 0, 1];
$hash{$key1} = [ 0, 1];
$hash{$key2} = [ 0, 1,2];
$hash{$key3} = [ 0];


thanks
 
J

jtbutler78

Cool thanks.

What about just say I wanted the size of $hash{baz} => [ 0,1,2 ] ?


I have a hash with the values being arrays. How can I find the size of
these arrays?

example:
$hash{$key} = [ 0, 1];
$hash{$key1} = [ 0, 1];
$hash{$key2} = [ 0, 1,2];
$hash{$key3} = [ 0];

#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;

my %hash = ( foo => [ 0,1 ] ,
bar => [ 0,1 ] ,
baz => [ 0,1,2 ],
waldo => [ 0] );

my $sum;
$sum += @$_ for values %hash;

print "total length = $sum\n";

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Lawrence Statton - (e-mail address removed) s/aba/c/g
Computer software consists of only two components: ones and
zeros, in roughly equal proportions. All that is required is to
sort them into the correct order.
 
I

it_says_BALLS_on_your forehead

I have a hash with the values being arrays. How can I find the size of
these arrays?

example:
$hash{$key} = [ 0, 1];
$hash{$key1} = [ 0, 1];
$hash{$key2} = [ 0, 1,2];
$hash{$key3} = [ 0];


use strict;
use warnings;

my %hash = (
one => [0],
two => [1,2],
three => [4,5,6],
);

map { my $size = @{$hash{$_}}; print "$_ => $size\n" } keys %hash;
 
P

Paul Lalli

What about just say I wanted the size of $hash{baz} => [ 0,1,2 ] ?

Any list evaluated in scalar context returns its size.

No. Any *array* evaluated in scalar context returns its size. A
"list" in scalar context doesn't exist. The comma-operator in scalar
context returns its right-most argument.
@$hash{baz} is the list ( 0, 1, 2 )

No, it's not. @$hash{baz} is the same as @{$hash}{baz}, which is the
one-element slice of the hash %{$hash} containing the element 'baz'.
I'm guessing you meant @{$hash{$baz}}, which is the array referenced by
the hash value $hash{$baz}.

You need to re-read the dereferencing rules in
perldoc perlref

Even so, that would be the *array* containing the list (0, 1, 2). The
distinction is important. Please read:
perldoc -q difference
Therefore

my $size = @$hash{baz};

my $size = @{$hash{baz}};
You can use scalar() to force scalar context in places where that is
needful.

print 'The array has ', scalar(@$hash{baz}), ' element(s)';

Or just use an operator that does force scalar context, instead of one
that gives list context:

print 'The array has ' . @{$hash{baz}} . ' element(s)';

Paul Lalli
 
T

Tad McClellan

I have a hash with the values being arrays. How can I find the size of
these arrays?
example:
$hash{$key} = [ 0, 1];


But using "Use Rule 1" from:

perldoc perlreftut


I like to do it in 3 steps:

my $size = @array; # pretend it is a plain array

my $size = @{ }; # replace the array's *name* with a block

my $size = @{ $hash{$key} }; # put something in the block that
# returns the right type of reference
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top