1
2
3
4
5
my @array = (1..15);
my @result = grep {$_ > 5 && $_ < 12}@array;
print join (", ", @result);
1
2
3
4
5
6
7
my @array = (1..15);
foreach (@array) # HIER scheint das Problem zu sein
{
print $_ if ($_ > 5 && $_ < 12); #Das hier spielt keine Rolle
}
print join (", ", @array);
1
2
3
4
5
6
7
8
9
10
11
my @ungerade;
foreach (1..500)
{
if ($_ % 2)
{
push (@ungerade, $_);
}
}
print "\n\n Ungerade Zahlen von 1 bis 500: \n";
print join ("\t", @ungerade);
1
2
3
print "\n\n Das selbe in grep: \n";
my @ungerade = grep { $_ % 2; } 1..500;
print join ("\t", @ungerade);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#!/usr/bin/perl use warnings; use strict; my @array = (1 .. 15); my @result = (); my $i; foreach $i (@array) { if ($i > 5 && $i < 12) { push(@result, $i); } } foreach $i (@result) { print "$i\n"; }
1
2
3
4
5
6
7
my @array = (1..15);
foreach (@array) {
print $_ if ($_ > 5 && $_ < 12);
}
print join (", ", @array);