Thread $rofl =~ /hallo{2,}/i (4 answers)
Opened by Gast at 2007-07-18 15:49

shigetsu
 2007-07-20 04:54
#78600 #78600
User since
2007-04-22
16 Artikel
BenutzerIn
[Homepage] [default_avatar]
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
#!/usr/bin/perl -l

use strict;
use warnings;

use Data::Dumper qw(Dumper);

{
    my $boundary = 2;
    my $keyword = 'hallo';

    my %table = map { $_ => join ' ', ($keyword, 'foo') x $_ } (0, 1, 2, 3, 4);

    my $does_match = sub {
        my ($str) = @_;

        my $occurences = 0;
        $occurences++ while $str =~ /(?:\A|\b)$keyword(?:\b|\z)/g;

        return { occurences => $occurences, match => ($occurences >= $boundary) ? 'yes' : 'no' };
    };

    print Dumper \%table;

    foreach my $often (sort { $a <=> $b } keys %table) {
        my $retval = $does_match->($table{$often});
        print "occurences: $retval->{occurences}, match: $retval->{match}";
    }
}

Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
$VAR1 = {
'4' => 'hallo foo hallo foo hallo foo hallo foo',
'1' => 'hallo foo',
'3' => 'hallo foo hallo foo hallo foo',
'0' => '',
'2' => 'hallo foo hallo foo'
};

occurences: 0, match: no
occurences: 1, match: no
occurences: 2, match: yes
occurences: 3, match: yes
occurences: 4, match: yes

View full thread $rofl =~ /hallo{2,}/i