I want to apply a function to every item of a list and store results similar to map(function, list)
in python.
Tried to pass a function to map, but got this error:
perl -le 'my $s = sub {}; @r = map $s 0..9'
panic: ck_grep at -e line 1.
What's the proper way to do this?
my $squared = sub {
my $arg = shift();
return $arg ** 2;
};
then either
my @list = map { &$squared($_) } 0 .. 12;
or
my @list = map { $squared->($_) } 0 .. 12;
or maybe
my $squared;
BEGIN {
*Squared = $squared = sub(_) {
my $arg = shift();
return $arg ** 2;
};
}
my @list = map { Squared } 0 .. 12;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With