in reply to hmap revisited
Change
my %new2 = hmap { $_[1]{nick} =~ /rbush|fred/ ? ($_[0], hmap { $_[0], $_[1] } %{$_[1]}) : () } %$h;
to
my %new2 = hmap { $_[1]{nick} =~ /rbush|fred/ ? $_[0] => { hmap { $_[0], $_[1] } %{$_[1]} } : () } %$h;
The relevant change is the curlies around the inner call to hmap. %{$_[1]} creates a list of the hash's content from the hash reference. You have to mirror that on the output side by creating a hash reference from the list.
You can't have it both ways. hmap can either return a list that can be used to initialize a hash or a hash reference.
An alternative would be to just pass hash references instead of the contents of hashes.
sub hmap (&$) { my $code = shift; my @i = %{ shift() }; my @rv; push @rv, $code->(shift @i,shift @i) while @i; return { @rv }; } my $new2 = hmap { $_[1]{nick} =~ /rbush|fred/ ? $_[0] => hmap { $_[0], $_[1] } $_[1] : () } $h;
(Removed the useless local @_ and @_=. What do you think calling a sub does...)
Since we have an actual hash to work with instead of a list of its content, we can use each instead of flattening the hash into @i.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: hmap revisited
by zerohero (Monk) on Feb 01, 2009 at 00:49 UTC | |
by ikegami (Patriarch) on Feb 01, 2009 at 05:21 UTC | |
|
Re^2: hmap revisited
by zerohero (Monk) on Feb 01, 2009 at 19:21 UTC | |
by ikegami (Patriarch) on Feb 01, 2009 at 19:49 UTC |