Counting elements in a hash

R

russell.brooks

Hi,
I've created a hash of an array. There are a varying number of
elements in the arrays. I need to count the number of elements, and
although I have read through the documentation, I have been unable to
find a clean way of doing this.
For example,

$table{28}[0] = 541;
$table{36}[0] = 1024;
$table{36}[1] = 2048;
$table{36}[2] = 4096;
$table{36}[3] = 8192;
$table{51}[0] = 77;

What I'm trying to obtain is a count of values for each array in the
hash. For example, the above example has 4 values associated with
array 36.

scalar keys %table gives 3, which isn't what I need.
scalar keys $tables{28} is illegal since the arguments for keys must
be a hash, not a hash element.

Is iterating through the hash the best option?

Thanks,
Russ
 
P

Paul Lalli

Hi,
I've created a hash of an array. There are a varying number of
elements in the arrays. I need to count the number of elements, and
although I have read through the documentation, I have been unable to
find a clean way of doing this.
For example,

$table{28}[0] = 541;
$table{36}[0] = 1024;
$table{36}[1] = 2048;
$table{36}[2] = 4096;
$table{36}[3] = 8192;
$table{51}[0] = 77;

What I'm trying to obtain is a count of values for each array in the
hash. For example, the above example has 4 values associated with
array 36.

scalar keys %table gives 3, which isn't what I need.
scalar keys $tables{28} is illegal since the arguments for keys must
be a hash, not a hash element.

Is iterating through the hash the best option?

You've made this more complicated than it is. $table{36} is an array
reference. You want to find the number of elements in the array
referenced by $table{36}, correct?

$size = @{$table{36}};

The @{ } syntax dereferences the given array reference. The assignment to
a scalar evaluates the resulting array in a scalar context, which results
in the size of that array.

Paul Lalli
 
D

David K. Wall

Hi,
I've created a hash of an array. There are a varying number of
elements in the arrays. I need to count the number of elements,
and although I have read through the documentation, I have been
unable to find a clean way of doing this.
For example,

$table{28}[0] = 541;
$table{36}[0] = 1024;
$table{36}[1] = 2048;
$table{36}[2] = 4096;
$table{36}[3] = 8192;
$table{51}[0] = 77;

What I'm trying to obtain is a count of values for each array in
the hash. For example, the above example has 4 values associated
with array 36.

scalar keys %table gives 3, which isn't what I need.
scalar keys $tables{28} is illegal since the arguments for keys
must be a hash, not a hash element.

Is iterating through the hash the best option?

You want the size of an *array*, right? The way to do that is to put
the array in scalar context, but $table{28} is a reference to an
array. So dereference it and then put it in scalar context.

my $size = @{$table{36}};

or you can be more specific, in case the normal context is list
context:

print scalar @{$table{36}};

(without using scalar() this would just print the elements of
@{$table{36}} )
 
B

Brian McCauley

Christian Winter said:
my $count;
$count += scalar @{$table{$_}} for(keys %table);

Or, more simply:

my $count = 0; # Want 0 not undef if %table empty
$count += @$_ for values %table;

--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 
R

russell.brooks

Paul Lalli said:
Hi,
I've created a hash of an array. There are a varying number of
elements in the arrays. I need to count the number of elements, and
although I have read through the documentation, I have been unable to
find a clean way of doing this.
For example,

$table{28}[0] = 541;
$table{36}[0] = 1024;
$table{36}[1] = 2048;
$table{36}[2] = 4096;
$table{36}[3] = 8192;
$table{51}[0] = 77;

What I'm trying to obtain is a count of values for each array in the
hash. For example, the above example has 4 values associated with
array 36.

scalar keys %table gives 3, which isn't what I need.
scalar keys $tables{28} is illegal since the arguments for keys must
be a hash, not a hash element.

Is iterating through the hash the best option?

You've made this more complicated than it is. $table{36} is an array
reference. You want to find the number of elements in the array
referenced by $table{36}, correct?

$size = @{$table{36}};

The @{ } syntax dereferences the given array reference. The assignment to
a scalar evaluates the resulting array in a scalar context, which results
in the size of that array.

Paul Lalli

Thanks for the response. As you said, I was making it too complicated.

Russ
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top