Re: map2 {} grep2 {} ...
by BrowserUk (Patriarch) on Jul 14, 2011 at 12:13 UTC
|
sub map2(&@){
my $code = shift;
map $code->( shift, shift ), 0 .. $#_/2
}
sub grep2(&@){
my $code = shift;
map{
my @pair = (shift,shift);
$code->( @pair ) ? @pair : ()
} 0 .. $#_/2
}
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] |
|
Works like a charm! Thank you very much.
| [reply] |
|
An excellent suggestion. Fav’d. The best way to solve a problem is not just “a solution that works,” but, as here, “a solution that is abundantly clear.” Something that gives you, not only the ability to do something, but a way to point out to your successor what you are doing and why you are doing it ... to capture the original designer’s intent.
| |
Re: map2 {} grep2 {} ...
by Tux (Canon) on Jul 14, 2011 at 12:10 UTC
|
%h_numbers = map { looks_like_number ($h{$_}) ? ($_, $h{$_}) : () } keys %h;
%h_numbers = map { @$_ } grep { looks_like_numebr ($_->[1]) } map { [ $_, $h{$_} ] } keys %h;
Enjoy, Have FUN! H.Merijn
| [reply] [d/l] [select] |
Re: map2 {} grep2 {} ...
by moritz (Cardinal) on Jul 14, 2011 at 12:09 UTC
|
See List::MoreUtils, specifically pairwise (and natatime for the general case)
| [reply] [d/l] [select] |
|
| [reply] |
Re: map2 {} grep2 {} ...
by Anonymous Monk on Jul 14, 2011 at 12:09 UTC
|
Consider "each."
Also consider a while loop that shifts the first variable off an array in the "while" part, then shifts the remainders off during the body of the loop (using "last unless" as needed). | [reply] |
Re: map2 {} grep2 {} ...
by betterworld (Curate) on Jul 14, 2011 at 17:30 UTC
|
The solutions offered so far are very sophisticated, but I think the most stupid way is also the most readable:
my %h_prefixed;
my %h_numbers;
while(my ($key, $val) = each %h) {
$h_numbers{$key} = $val if looks_like_a_number($val);
$h_prefixed{$key} = "p$val";
}
And it's (probably) not even slower or anything.
For a (maybe a bit) less readable solution, I'd like to offer the following:
my %h_prefixed;
@h_prefixed{keys %h} = map "p$_", values %h;
| [reply] [d/l] [select] |
Re: map2 {} grep2 {} ...
by duelafn (Parson) on Jul 14, 2011 at 18:45 UTC
|
sub map2(&@) {
my $f = shift;
my @res;
no strict 'refs';
my $caller = caller;
local(*{$caller."::a"}) = \my $a;
local(*{$caller."::b"}) = \my $b;
if (defined(wantarray)) {
push @res, $f->($a,$b) while ($a, $b) = splice @_, 0, 2;
return @res;
} else {
$f->($a,$b) while ($a, $b) = splice @_, 0, 2;
}
}
# ...
%h_prefixed = map2 { ($a, "p$b") } %h;
@a_hashed = map2 { +{ $a => $b } } @a;
| [reply] [d/l] [select] |
Re: map2 {} grep2 {} ...
by JavaFan (Canon) on Jul 14, 2011 at 15:57 UTC
|
Some solutions that don't require a special map or grep:
%h_numbers = map {looks_like_number($h{$_}) ? ($_, $h{$_}) : ()} keys
+%h; # Or
%h_numbers = map {($_, $h{$_})} grep {looks_like_number($h{$_})} keys
+%h;
%h_prefixed = map {($_, "p".$h{$_})} keys %h; # Or
%h_prefixed = %h; $_ = "p$_" for values %h_prefixed;
# Both solutions below destroy @a; but you can make a copy first.
while(my($x,$y) = splice @a, 0, 2) {push @a_hashed, {$x, $y}} # Or
push @a_hashed, {shift @a, shift @a} while @a;
| [reply] [d/l] |
Re: map2 {} grep2 {} ...
by ikegami (Patriarch) on Jul 14, 2011 at 16:37 UTC
|
You don't even use the key in
my %h_prefixed = map2 { ($_[0], "p".$_[1]) } %h;
You could do
my %h_prefixed = %h;
$_ = "p$_" for values %h_prefixed;
| [reply] [d/l] [select] |
|
He *did* use the keys -- to construct the new hash.
And you also used the keys, when you constructed the new hash.
But you also used the values at point -- and then again when you modified them.
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] |