Perl: the Markov chain saw PerlMonks

### Counting unique(ish) elements in a hash

 on Aug 30, 2013 at 09:15 UTC Need Help??
Dr Manhattan has asked for the wisdom of the Perl Monks concerning the following question:

Hi all

This may seem a stupid question but I am really having some difficulty

I want to number each key inside a hash of a hash, but the counting have to be done only inside the first key(if that makes any sense)

For instance, if I have a hash with this type of layout:

%hash{ peter => sister => Mary -> 1 => Jane -> 1 => Kate -> 1 => brother => Jack -> 1 => parents => fater -> 1 => mother -> 1 Nick => sister => Jeny -1 }

What I want to do is number each entry under the first key, for instance my output should look like this:

Peter sister Mary 1 Peter sister Jane 2 Peter sister Kate 3 Peter brother Jack 4 Peter parents father 5 Peter parent mother 6 Nick sister Jenny 1

but I'm struggling because all of them are unique entries, or am I missing something obvious?

Thanx in advance for any help, much appreciated

Replies are listed 'Best First'.
Re: Counting unique(ish) elements in a hash
by kcott (Chancellor) on Aug 30, 2013 at 11:14 UTC

G'day Dr Manhattan,

Your problem description if far from clear, and you've posted no code that might have clarified what you meant, or given some indication of whatever it is that you're having difficulty with. I don't know what you mean by "unique(ish)" or "... struggling because all of them are unique ...".

Here's a guess at what I think your data looks like and how you might handle it:

\$ perl -Mstrict -Mwarnings -E ' use Data::Dumper; my %data = ( A => { a => {f => 1, g => 1, h => 1}, b => {i => 1}, c => {j => 1, k => 1}, }, B => { d => {l => 1, m => 1}, e => {n => 1}, }, ); for my \$key (keys %data) { my \$count = 0; map { \$_ = ++\$count for values \$data{\$key}{\$_} } keys \$data{\$k +ey}; } print Dumper \%data; ' \$VAR1 = { 'B' => { 'e' => { 'n' => 1 }, 'd' => { 'l' => 2, 'm' => 3 } }, 'A' => { 'b' => { 'i' => 1 }, 'a' => { 'h' => 2, 'g' => 3, 'f' => 4 }, 'c' => { 'j' => 5, 'k' => 6 } } };

-- Ken

Re: Counting unique(ish) elements in a hash
by hdb (Prior) on Aug 30, 2013 at 10:32 UTC

As you have not given us a correct Perl structure for your data, a complete answer cannot be given. However, the code could look something like the one below, where you need to enter the correct pieces to loop over the entries under your keys yourself (replacing the ... with the appropriate (nested) loop).

for my \$name ( keys %hash ) { my \$count = 0; for my \$entry ( ... ) { # loop over entries under first key \$count++; print "\$name \$entry \$count\n"; } }
Re: Counting unique(ish) elements in a hash
by Anonymous Monk on Aug 30, 2013 at 09:45 UTC

For instance, if I have a hash with this type of layout:

Thats syntax errors, not a hash. So first step, drop the hash. :) then

name, relative, ship peter, mary, sister peter, jane, sister peter, kate, sister ...

If you really want help with modifying your existing hash, post real code

Re: Counting unique(ish) elements in a hash
by marinersk (Priest) on Aug 30, 2013 at 19:00 UTC
my \$sequenceNumber = 0; foreach my \$keyValue (keys %familyInfo) { \$sequenceNumber++; # Do something with the sequence number }

Create A New User
Node Status?
node history
Node Type: perlquestion [id://1051581]
Approved by Corion
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (13)
As of 2017-04-25 15:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
I'm a fool:

Results (456 votes). Check out past polls.