in reply to How to create multiple keys in a hash

Will a nested hash structure do what you want?

$somehash{foo}{bar}{baz}++; $somehash{quux}{wibble}{42}++; foreach $x (keys %somehash) { foreach $y (keys %{$somehash{$x}}) { foreach $z (keys %{$somehash{$x}{$y}}) { print "There were $somehash{$x}{$y}{$z} ". "occurrances of $x,$y,$z\n"; } } }

$;=sub{$/};@;=map{my($a,$b)=($_,$;);$;=sub{$a.$b->()}} split//,".rekcah lreP rehtona tsuJ";$\=$ ;->();print$/

Replies are listed 'Best First'.
Re: Re: How to create multiple keys in a hash
by tanuja (Initiate) on Sep 21, 2003 at 00:47 UTC
    Hi Jonadab, I have tried this code but it doesnt work and doesnt give me any output. Please let me know. Thanks.
      I have tried this code but it doesnt work and doesnt give me any output.

      It works for me here. Here's what it does under Perl 5.6 on Linux:

      [family@pedestrian family]$ perl -e ' > $somehash{foo}{bar}{baz}++; > $somehash{quux}{wibble}{42}++; > > foreach $x (keys %somehash) { > foreach $y (keys %{$somehash{$x}}) { > foreach $z (keys %{$somehash{$x}{$y}}) { > print "There were $somehash{$x}{$y}{$z} ". > "occurrances of $x,$y,$z\n"; > } > } > } > > ' There were 1 occurrances of foo,bar,baz There were 1 occurrances of quux,wibble,42 [family@pedestrian family]$

      update: also works on ActiveState Perl (5.8) on WinMe:

      F:\perl>perl temp.pl There were 1 occurrances of quux,wibble,42 There were 1 occurrances of foo,bar,baz

      So if it doesn't work for you, please post a reply with a copy-and-paste showing what you tried and what error message you got.


      $;=sub{$/};@;=map{my($a,$b)=($_,$;);$;=sub{$a.$b->()}} split//,".rekcah lreP rehtona tsuJ";$\=$ ;->();print$/