Schrift
[thread]7584[/thread]

regular expressions



<< >> 10 Einträge, 1 Seite
paidopoieo
 2006-01-03 02:41
#61439 #61439
User since
2005-12-02
96 Artikel
BenutzerIn
[default_avatar]
Hallo Leute,
ich habe einen string und moechte ueberpruefen, z.b: ob er mindestens zwei k, oder zwei r und zwei w enthaelt...
oder ein k und ein r und zwei w enthaelt....

vielen dank im voraus...
Hubert
esskar
 2006-01-03 11:33
#61440 #61440
User since
2003-08-04
7321 Artikel
ModeratorIn

user image
ohne regexp
Code: (dl )
1
2
3
4
my @chars = split //, $string;
my $h = ();
$h{$_}++ foreach @chars;
print "True" if $h{k} >= 2 || $h{r} == 2 && $h{w} == 2 || $h{k} == 1 && $h{r} == 1 && $h{w} == 2;
pq
 2006-01-03 11:35
#61441 #61441
User since
2003-08-04
12208 Artikel
Admin1
[Homepage]
user image
Code: (dl )
1
2
3
for ($string) {
  print "yes" if tr/k// >= 2 and tr/r// >= 2 and tr/w// >= 2;
}
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. -- Damian Conway in "Perl Best Practices"
lesen: Wiki:Wie frage ich & perlintro Wiki:brian's Leitfaden für jedes Perl-Problem
esskar
 2006-01-03 15:58
#61442 #61442
User since
2003-08-04
7321 Artikel
ModeratorIn

user image
schon bemerkenswert

Code: (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
use strict;
use warnings;
use Benchmark;

my @strings;

my @chars = 'a'..'z';

for (1..10) {
my $len = int ( rand(100)) + 10;

my $string;
for(1 .. $len) {
$string .= $chars[int ( rand(@chars) )];
}

push @strings, $string;
}

print scalar @strings, " strings created !\n";

my $iterations = 10000;
my $count = 0;

timethese(
$iterations,
{
"esskar" => sub {
foreach (@strings) {
my @chars = split //, $_;
my %h = ();
$h{$_}++ foreach @chars;

$count += is_true($h{k}, $h{r}, $h{w});
}
}
}
);

print "esskar: $count\n";

$count = 0;
timethese(
$iterations,
{
"pq" => sub {
foreach (@strings) {
$count += is_true( tr/k//, tr/r//, tr/w// );
}
}
}
);

print "pq: $count\n";

sub is_true {
my ($k, $r, $w) = @_;

$k ||= 0;
$r ||= 0;
$w ||= 0;

return $k >= 2 || $r == 2 && $w == 2 || $k == 1 && $r == 1 && $w == 2;
}
^Z
10 strings created !
Benchmark: timing 10000 iterations of esskar...
esskar: 15 wallclock secs (14.47 usr + 0.00 sys = 14.47 CPU) @ 691.13/s (n=10000)
esskar: 60000
Benchmark: timing 10000 iterations of pq...
pq: 0 wallclock secs ( 0.34 usr + 0.00 sys = 0.34 CPU) @ 29069.77/s (n=10000)
(warning: too few iterations for a reliable count)
pq: 60000


obwohl doch tr auch zählen muss ... eigentlich sogar 3mal... wo ist mein denkfehler ?
pq
 2006-01-03 16:19
#61443 #61443
User since
2003-08-04
12208 Artikel
Admin1
[Homepage]
user image
wieso kommt es plötzlich in mode, immer zweimal timethese aufzurufen?
ich mach das immer in einem aufruf.
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. -- Damian Conway in "Perl Best Practices"
lesen: Wiki:Wie frage ich & perlintro Wiki:brian's Leitfaden für jedes Perl-Problem
esskar
 2006-01-03 17:23
#61444 #61444
User since
2003-08-04
7321 Artikel
ModeratorIn

user image
[quote=pq,03.01.2006, 15:19]wieso kommt es plötzlich in mode, immer zweimal timethese aufzurufen?
ich mach das immer in einem aufruf.[/quote]
hatte einfach kopiert! :)
pq
 2006-01-03 20:52
#61445 #61445
User since
2003-08-04
12208 Artikel
Admin1
[Homepage]
user image
[quote=esskar,03.01.2006, 16:23]hatte einfach kopiert! :)[/quote]
soso, sowas hatte ich mir schon gedacht =)
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. -- Damian Conway in "Perl Best Practices"
lesen: Wiki:Wie frage ich & perlintro Wiki:brian's Leitfaden für jedes Perl-Problem
paidopoieo
 2006-01-04 01:15
#61446 #61446
User since
2005-12-02
96 Artikel
BenutzerIn
[default_avatar]
hi,
dankeschoen, ward mir eine sehr grosse Hilfe,

vielen herzlichen dank

Hubert
pKai
 2006-01-08 02:37
#61447 #61447
User since
2005-02-18
357 Artikel
BenutzerIn
[default_avatar]
[quote=esskar,03.Jan..2006, 14:58]obwohl doch tr auch zählen muss ... eigentlich sogar 3mal... wo ist mein denkfehler ?[/quote]
@esskar: Diese Ausführungen scheinen eine gute Erklärung für das gute Abschneiden von tr zu geben.
I sense a soul in search of answers.
esskar
 2006-01-09 01:36
#61448 #61448
User since
2003-08-04
7321 Artikel
ModeratorIn

user image
okay... aber der unterschied ist schon extrem
<< >> 10 Einträge, 1 Seite



View all threads created 2006-01-03 02:41.