Thread Map Verständnisproblem? (26 answers)
Opened by crojay at 2011-06-16 00:28

Linuxer
 2011-07-08 19:13
#150248 #150248
User since
2006-01-27
3890 Artikel
HausmeisterIn

user image
Hallo renee,

sorry, ich bin Dir ja noch eine Antwort mit Code schuldig. Das sei hiermit (später als geplant) nachgetragen:

more (16.7kb):

Code (perl): (dl )
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/perl
# vim: set ts=4 sw=4 et sta:
use strict;
use warnings;

use Benchmark qw( cmpthese );

use Digest::MD5 qw( md5_base64 );


# https://www.perl-community.de/bat/poard/thread/16419

my @unfiltered = (<DATA>) x 100;


# original
sub map_grep1 {

    my @filtered =
      map { my $x = $_; chomp $x; $x =~ s/->\s*(.*)/$1/g; $x}
        grep { ! m/(^\s*([#].*)?)$/ }
          @unfiltered;

    return @filtered;
}

# linuxer
sub map_grep2 {

    my @filtered =
      map { m/->\s*(.*)/ ? $1 : $_ }
        grep { ! m/^\s*(?:#|$)/ }
          @unfiltered;
    chomp @filtered;

    return @filtered;
}

# wer grep
sub map_grep3a {

    my @filtered =
      map { s/^->\s*(.*)/$1/; $_ }
        grep { ! m/^\s*(?:#|$)/ }
          @unfiltered;
    chomp @filtered;

    return @filtered;
}

# wer grep+map
sub map_grep3b {

    my @filtered =
      map { s/^->\s*(.*)/$1/ ; m/^\s*(?:#|$)/ ? () : $_ }
          @unfiltered;
    chomp @filtered;

    return @filtered;
}

# renee grep+map
sub map_grep3c {

    my @filtered =
      map { s/^->\s*// ; m/^\s*(?:#|$)/ ? () : $_ }
          @unfiltered;
    chomp @filtered;

    return @filtered;
}

#print join $/, map_grep3b(), $/;
#exit;



my %check = (
        'original'       => \&map_grep1,
        'linuxer'        => \&map_grep2,
        'wer_map'        => \&map_grep3a,
        'wer_map+grep'   => \&map_grep3b,
        'renee_map+grep' => \&map_grep3c,
);

# BENCHMARK
cmpthese( -1, \%check );


# checks for correct data
for my $k ( keys %check ) {
    # print to see if we test different sub routines
    printf "%15s : %15s %s \n", $k, $check{$k}, md5_base64( join $/, $check{$k}->());
}

# While testing, these conditions gave different results for "original" and "linuxer"
# - do a second comparison (second comparison shows different results)
# - comparison after for loop (comparison gives different result to a run before the for-loop or without it)
#cmpthese( -1, \%check );

__DATA__
# comment 1

.*regex 1
-> file path 1

# comment 2
.+regex 2
-> file path 2

meine Beiträge: I.d.R. alle Angaben ohne Gewähr und auf Linux abgestimmt!
Die Sprache heisst Perl, nicht PERL. - Bitte Crossposts als solche kenntlich machen!

View full thread Map Verständnisproblem?