Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re: How to create multiple keys in a hash

by bl0rf (Pilgrim)
on Sep 21, 2003 at 00:31 UTC ( [id://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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://292932]
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-20 02:20 GMT
Find Nodes?
    Voting Booth?

    No recent polls found