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

Hi Monks,

If I read a file and make an structure like the following. How can I print out and count the number of elements in this hash. Remember each key has two values and in this case each key may repeat over and over.

push @{ $somehash{$key} }, { 'start' => $start, 'end' => $end }

Thanks, Pedro

Replies are listed 'Best First'.
Re: access and printing of array of hash?
by kyle (Abbot) on Jan 28, 2009 at 21:04 UTC

    This might help: How can I visualize my complex data structure? (short answer: Data::Dumper).

    If you want to know how many hashes with the start/end keys you have, this should do it.

    my $count = map { @{ $_ } } values %somehash; # longer way use List::Util 'sum'; my $count = sum map { scalar @{ $_ } } values %somehash;

    If that's not the number you're looking for, then I'm not sure what is. Can you explain further?

      Thanks Kyle, That solved my problem.
Re: access and printing of array of hash?
by moritz (Cardinal) on Jan 28, 2009 at 21:01 UTC
    use Data::Dumper; print Dumper \%somehash; my $count = 0; $count += 2 * @$_ for values %somehash;

    If you want a different output format, tell us what you want, and what you've tried so far.

    Note that it only counts the number of pairs in the inner hashes - if you want something different... well, you know the drill by now.

Re: access and printing of array of hash?
by sweetblood (Prior) on Jan 28, 2009 at 22:41 UTC
    You really should check out perldoc perldsc the Data Structure Cookbook it great for this kind of thing!

    Sweetblood