mask_man has asked for the wisdom of the Perl Monks concerning the following question:
And the result isuse strict; use warnings; my $hashref = { normal_enemies => { cloud_guy => { attacktype => 'thunder', power => 4, hp => 5, }, rain_man => { attacktype => 'water', power => 3, hp => 7, }, flame_kid => { attacktype => 'fire', power => 6, hp => 4, }, }, boss_enemies => { large_shark => { attacktype => 'water'. power => 9, hp => 13, special => 'none', }, wingding_monster => { attacktype => 'font'. power => 24, hp => 23, special => 'encrypt', }, green_dragon => { attacktype => 'fire'. power => 8, hp => 58, special => 'fire breath', }, }, items => { weapons => { sword_chucks => { power => 28, accuracy => 6, recommended_class => 'Crazy Fighter', }, cane => { power => 7, accuracy => 3, recommended_class => 'The Mage That Just Uses Magic. Y +ou know, if he ever needs to use this thing you\'re screwed anyway, r +ight?', }, katana => { power => 'over nine-thousand', accuracy => 8, recommended_class => 'Ninja Assassin', }, }, armor => { helm => { body_part => 'head', defense => 2.5, }, guantlets => { body_part => 'hands', defense => 1, }, necklace => { body_part => 'neck', defense => .1337, }, }, }, }; my %hash = %$hashref; print "Normal(Should fail with 'HASH(0xhex)' - $hashref\n"; print "Dereferenced - \n"; foreach (keys %hash) { print "$_ - $hash{$_}\n"; }
Now, what I want to do is take %hash deeper into the hashref. For example, how would I get a list of normal_enemies or the information from sword_chucks(this should come out as 'power - 28' instead of 'power - HASH(junk)') ? Thanks in advanced.Normal(Should fail with 'HASH(0xhex)' - HASH(0x99aae4) Dereferenced - normal_enemies - HASH(0x98a814) boss_enemies - HASH(0x99adc4) items - HASH(0x99ab24)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: getting keys of multi-level hashrefs
by Neighbour (Friar) on Jul 07, 2010 at 09:24 UTC | |
by cdarke (Prior) on Jul 07, 2010 at 10:13 UTC | |
by Neighbour (Friar) on Jul 07, 2010 at 11:24 UTC | |
by mask_man (Initiate) on Jul 07, 2010 at 20:12 UTC | |
by Neighbour (Friar) on Jul 08, 2010 at 09:50 UTC | |
|
Re: getting keys of multi-level hashrefs
by jethro (Monsignor) on Jul 07, 2010 at 09:56 UTC | |
|
Re: getting keys of multi-level hashrefs
by suhailck (Friar) on Jul 07, 2010 at 09:28 UTC | |
|
Re: getting keys of multi-level hashrefs
by youwin (Beadle) on Jul 07, 2010 at 22:11 UTC |