BiffBaker has asked for the wisdom of the Perl Monks concerning the following question:
My system uses arrayrefs of arrayrefs containing what are in effect key-value pairs, e.g.:
$pairs = [ ['fish','fry'], ['clam','bake'], ['wiener','roast'], ];
It uses this data structure rather than a hash because it works better with the algorithms used in some places. I would like to access the data, however, as if it were a hashref, with similar syntax.
Here is a construct I came up with:
my $party = {map{@$_}@$pairs}->{'fish'};
The value 'fry' will be assigned to $party.
The one caution is that if there is more than one element in $pairs with the key 'fish' (there should not be, but...), there may be problems. I believe the last element in the array with this key will be used.
Does anyone have thoughts on this? Is there a better way to do what I am describing?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Pushing arrays into hashes
by Roy Johnson (Monsignor) on Jun 22, 2007 at 18:12 UTC | |
by BiffBaker (Novice) on Jun 22, 2007 at 23:42 UTC | |
|
Re: Pushing arrays into hashes
by guinex (Sexton) on Jun 22, 2007 at 18:54 UTC | |
|
Re: Pushing arrays into hashes
by Jenda (Abbot) on Jun 23, 2007 at 19:12 UTC | |
by BiffBaker (Novice) on Jun 26, 2007 at 01:02 UTC |