Coruscate has asked for the wisdom of the Perl Monks concerning the following question:
What I have working at the moment is a hash that is setup with two embedded map{} statements. This setup allows you to declare multiple hash keys to have the same value in a eye-pleasing manner. Now what I need to accomplish is one step further: populating this hash from an array with elements that will be splitted to extract the data to place into the hash.
Here's the code I have so far (working code):
my %hash = map { my $item = pop @$_; map { $_, $item } @$_ } [ ( 'key1', 'key2', 'key3' ) => 'Value for key1, key2, and key3' ], [ qw( key4 key5 key6 ) => 'Value for key4, key5, key6' ], [ ( 'key7', 'key8', 'key9' ) => 'Value for key7, key8, key9' ]; # So we can now do the following. Hopefully everyone # now understands what the hash declaration above does # Output shown after __END__ while ( my($k,$v) = each %hash ) { print "$k: $v\n"; } __END__ key1: Value for key1, key2, key3 key2: Value for key1, key2, key3 key3: Value for key1, key2, key3 key4: Value for key4, key5, key6 key5: Value for key4, key5, key6 etc etc etc
Okay perfect! I can now set multiple hash keys to contain the same value. This is precisely what I needed and I got that working. Now, the tougher part that I simply cannot grasp onto? How to fill that same hash, populating it from an array. The ideal array for my case here would be the key names separated by a tilde ('~'), and the value for those keys to be at the end of the element, separated from the key names by a newline ('\n'). I could live with there needing to be a tilde before the newline. So the array for the above example would look exactly like this:
my @data = ( qq{key1~key2~key3 Value for key1, key2, key3}, qq{key4~key5~key6 Value for key4, key5, key6}, qq{key7~key8~key9 Value for key7, key8, key9} );
Any suggestions as to how one would take the above array and create the hash I posted even further above? I know exactly how I want everything, now if I only had the brain power to figure it all out :)
C:\>shutdown -s
>> Could not shut down computer:
>> Microsoft is logged in remotely.
update (broquaint): title change (was Split array to populate a hash with multiple keys pointing to same value)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Splitting an array to populate hash
by Pardus (Pilgrim) on Jan 29, 2003 at 10:51 UTC | |
|
Re: Splitting an array to populate hash
by bart (Canon) on Jan 29, 2003 at 10:57 UTC | |
|
Re: Splitting an array to populate hash
by BrowserUk (Patriarch) on Jan 29, 2003 at 11:21 UTC | |
|
Re: Splitting an array to populate hash
by broquaint (Abbot) on Jan 29, 2003 at 11:15 UTC | |
|
Re: Splitting an array to populate hash
by Aragorn (Curate) on Jan 29, 2003 at 10:59 UTC | |
|
Re: Splitting an array to populate hash
by Hofmator (Curate) on Jan 29, 2003 at 12:52 UTC |