Apply a callback to an array: Difference between revisions

Content added Content deleted
m (add to Iteration cat)
m (→‎{{header|Perl}}: migrate content from Loop Structures)
Line 798: Line 798:
</lang>
</lang>
=={{header|Perl}}==
=={{header|Perl}}==
<lang perl>
<lang perl># create array
# create array
my @a = (1, 2, 3, 4, 5);
my @a = (1, 2, 3, 4, 5);


Line 827: Line 826:
my $func = \&mycallback;
my $func = \&mycallback;
my @d = map $func->($_), @a; # @d is now (2, 4, 6, 8, 10)
my @d = map $func->($_), @a; # @d is now (2, 4, 6, 8, 10)

</lang>
# filter an array
my @e = grep { $_ % 2 == 0 } @a; # @e is now (2, 4)</lang>


=={{header|PHP}}==
=={{header|PHP}}==