> How can I achieve a similar result using map?
not "really", to do so you would need to use map in void context in order to act like foreach!
DB<163> map { ($a,$b)=split /\s+/; $hash{$a}{$b}=1 } <I>; => (1, 1, 1, 1) DB<164> \%hash => { Canberra => { 12 => 1, 18 => 1 }, Sydney => { 12 => 1, 14 => 1 } + }
(Please note that you are calling split twice, I avoided this by setting $a and $b and have full control over the delimiter.)
You can't have map returning nested hashes assigned to a top-level hash, otherwise entries from different lines would collide (2 x Sidney, 2 x Canberra) and be overwritten:
%hash = map { entry1 => { entry2 => 1} } <> # collisions of entry1
Another concise way to do it, involving a "real" map and avoiding named variables:
DB<143> $hash{$_->[0]}{$_->[1]} = 1 for map { [split /\s+/] } <I> => "" DB<144> \%hash => { Canberra => { 12 => 1, 18 => 1 }, Sydney => { 12 => 1, 14 => 1 } + }
Of course creating an anonymous array is not optimal...
Cheers Rolf
( addicted to the Perl Programming Language)
In reply to Re: Possible for Map to create Hash of Hash?
by LanX
in thread Possible for Map to create Hash of Hash?
by konnjuta
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |