my @numbers = (0, 0.5, 1, 1.5, 2, 2.5);
# Calling grep with a BLOCK:
my @integers = grep {int($_) == $_} @numbers;
print join(", ", @integers), " are integers.\n";
# Calling grep with a function name:
my @basket = ("apple", undef, undef, undef, "banana", "cherry", undef, "date");
print "The number of elements in \@basket is ", scalar(@basket), "\n";
my @basket_1 = grep defined, @basket;
print "The number of *defined* elements in \@basket is ", scalar(@basket_1), "\n";
####
my @numbers = (0, 0.5, 1, 1.5, 2, 2.5);
# Calling grep with a BLOCK:
my @integers;
for (@numbers) {
push @integers, $_ if int($_) == $_;
}
print join(", ", @integers), " are integers.\n";
# Calling grep with a function name:
my @basket = ("apple", undef, undef, undef, "banana", "cherry", undef, "date");
print "The number of elements in \@basket is ", scalar(@basket), "\n";
my @basket_1;
for (@basket) {
push @basket_1, $_ if defined;
}
print "The number of *defined* elements in \@basket is ", scalar(@basket_1), "\n";
##
##
my @numbers = 1..10;
my @times_ten = map { $_ * 10 } @numbers;
print join(", ", @times_ten), "\n";