Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Counting the elements of a hash of arrays of arrays

by Clownburner (Monk)
on May 14, 2003 at 18:40 UTC ( [id://258198]=perlquestion: print w/replies, xml ) Need Help??

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

Oh wise monks,

I have toiled at this question and can't seem to get it.

I have a data structure that's a hash consisting of an array of arrays, created like this:

push @{$results{$addr}},[@output];
I need to get the count of how many elements are in the lowest level of this structure (that is, how many @outputs are there in the hash $results{$addr}).

I've tried a lot of solutions, and none seem to work. This:

my @a = $results{$addr};
returns an array with a single element, that then contains all the @output's. But if I try to 'scalar @a' I get '1', and if I scalar @a[0] or scalar($a[0]) I get a bunch of ARRAY refs.

$a = $results{$addr}[0] returns what appears to be an array ref in $a, but scalar(@$a) doesn't return anything, oddly.

No more direct manipulation of $results{$addr} seems to result in anything usable... If I do scalar @$results{$addr} I get 'Variable "$results" is not imported'. If I try print scalar $results{$addr} I get a list of array refs again.

I'm quite frustrated and though I feel I'm missing something simple, I can't figure out what the heck it might be.

All help appreciated.
"Non sequitur. Your facts are un-coordinated." - Nomad

Replies are listed 'Best First'.
Re: Counting the elements of a hash of arrays of arrays
by broquaint (Abbot) on May 14, 2003 at 18:46 UTC
      This just about did it. As it turns out I had to do scalar @{ $results{$addr} } to get the count of the @outputs, but it's brilliant.

      Now why couldn't I think of that? :-P
      "Non sequitur. Your facts are un-coordinated." - Nomad
Re: Counting the elements of a hash of arrays of arrays
by bigj (Monk) on May 14, 2003 at 18:46 UTC
    my @a = $results{$addr};
    returns an array with a single element, that then contains all the @output's. But if I try to 'scalar @a' I get '1', and if I scalar @a[0] or scalar($a[0]) I get a bunch of ARRAY refs.

    Try instead the same what you you have done to push at the array.

    my @a = @{$result{$addr}};

    Greetings,
    Janek

Re: Counting the elements of a hash of arrays of arrays
by artist (Parson) on May 14, 2003 at 18:50 UTC
    This small program should help you. Use Data::Dumper when in doubt.

    use Data::Dumper; use strict; my @output = (1,2,3); my $addr = 'x'; my %results; push @{$results{$addr}},[@output]; @output = (4,5); $addr = 'y'; push @{$results{$addr}},[@output]; @output = (1,4,7,9,11); $addr = 'x'; push @{$results{$addr}},[@output]; print Dumper(\%results); my $sum; foreach my $addr (keys %results){ my $array_ref = $results{$addr}; foreach my $inner_array_ref (@{$array_ref}){ $sum += scalar @{$inner_array_ref}; } } print $sum;
    artist
Re: Counting the elements of a hash of arrays of arrays
by suaveant (Parson) on May 14, 2003 at 18:52 UTC
    if you want the number of outputs, you need a loop.
    my $count = 0; for(@{$results{$addr}}) { $count += @{$results{$addr}{$_}} } print "$count outputs\n";
    untested

                    - Ant
                    - Some of my best work - (1 2 3)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://258198]
Approved by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2024-04-24 02:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found