6 Einträge, 1 Seite |
1
2
3
4
5
6
7
my @array = (hans, susanne, peter);
for (@array) {
if (/susanne/) {
# element susanne rauslöschen
}
}
@array = grep { !m/susanne/ } @array;
1 2 3 4 5 6 7 8 9 10 11 12 13
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use Carp; my @list = qw{ Tim Bob Susanne Olli Nancy }; my $idx = $#list; $list[$idx] eq 'Susanne' and splice (@list, $idx, 1) while($idx-- > 0); print Dumper \@list;
1 2 3 4 5
for my $i (reverse (0..$idx)) { if ($list[$i] eq 'Susanne') { splice (@list, $i, 1); } }
1
2
3
4
5
6
@list has 17576 elements!
Rate grep_re splice_re splice_eq grep_eq
grep_re 24.6/s -- -36% -46% -53%
splice_re 38.6/s 57% -- -16% -27%
splice_eq 45.9/s 87% 19% -- -13%
grep_eq 52.8/s 115% 37% 15% --
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
#!/usr/bin/perl use strict; use warnings; use Benchmark qw( cmpthese ); my @list = ( ( 'AAA' .. 'ZZZ' ) x 1 ); my $pattern = 'WVX'; print "\@list has " . @list . " elements!\n"; cmpthese( -2, { 'grep_eq' => \&grepped_eq, 'splice_eq' => \&spliced_eq, 'grep_re' => \&grepped_re, 'splice_re' => \&spliced_re, } ); sub grepped_eq { my @aa = @list; @aa = grep { $_ eq $pattern } @aa; } sub spliced_eq { my @aa = @list; for my $i ( reverse 0 .. $#aa ) { splice @aa, $i, 1 if $aa[$i] eq $pattern; } } sub grepped_re { my @aa = @list; @aa = grep { m/\Q$pattern\E/ } @aa; } sub spliced_re { my @aa = @list; for my $i ( reverse 0 .. $#aa ) { splice @aa, $i, 1 if $aa[$i] =~ m/\Q$pattern\E/; } } __END__
6 Einträge, 1 Seite |