Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Re: How to create multiple keys in a hash

by bl0rf (Pilgrim)
on Sep 21, 2003 at 00:31 UTC ( #292932=note: print w/replies, xml ) Need Help??

in reply to How to create multiple keys in a hash

I'm not exactly sure what you want to do with the
modified hash implementation but you answered your
own question by using split() in your example code

Simply use a separated string as your key and iterate
over the values in the string as if they were keys:

%modhash = (); $modhash{ 'first:second:third' } = [ 'bob', 'bill', 'bo' ]; $modhash{ 'Canada:USA:China' } = [ 'beaver','eagle','Mao' ]; # two sample entries, notice the anonymous arrays foreach $key ( keys %modhash ) { @subkeys = split( /:/, $key ); for( $i = 0; $i < scalar @subkeys; $i++ ) { print "subkey-> $subkeys[$i]\t", "subvalue-> $modhash{$key}->[$i]\n"; } print "\n"; # separates different main keys }
This code is tested ( ActivePerl 5.6.0 ) and produces
this output:

subkey-> Canada subvalue-> beaver
subkey-> USA subvalue-> eagle
subkey-> China subvalue-> Mao

subkey-> first subvalue-> bob
subkey-> second subvalue-> bill
subkey-> third subvalue-> bo

I've got a feeling that you might accomplish your
task in an easier manner.

My Site

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://292932]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (3)
As of 2018-07-20 05:02 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (424 votes). Check out past polls.