in reply to Re^4: Simple Matching
in thread Simple Matching

How does this compare speed-wise:
>perl -wMstrict -le "my @suits = qw(D S C H); my $suits = join '', @suits; my $flush = qr{ ([$suits]) (?: [^$suits]+ \1){4} }xms; print $_, $_ =~ $flush ? ' flush' : ' nope' for '2H 3D 4C 5S 5H', '1H 2H 3H 4H 5H'; " 2H 3D 4C 5S 5H nope 1H 2H 3H 4H 5H flush
Update: Might be faster if possessive:
>perl -wMstrict -le "my @suits = qw(D S C H); my $suits = join '', @suits; my $flush = qr{ ([$suits]) (?: (?> [^$suits]+) \1){4} }xms; print $_, $_ =~ $flush ? ' flush' : ' nope' for '2H 3D 4C 5S 5H', '1H 2H 3H 4H 5H'; " 2H 3D 4C 5S 5H nope 1H 2H 3H 4H 5H flush
5.10 has possessive modifier:
>perl -wMstrict -le "my @suits = qw(D S C H); my $suits = join '', @suits; my $flush = qr{ ([$suits]) (?: [^$suits]++ \1){4} }xms; print $_, $_ =~ $flush ? ' flush' : ' nope' for '2H 3D 4C 5S 5H', '1H 2H 3H 4H 5H'; " 2H 3D 4C 5S 5H nope 1H 2H 3H 4H 5H flush
Bugfix: Changed  (?> [^$suits])+ to  (?> [^$suits]+) per ikegami Re^6: Simple Matching.

Replies are listed 'Best First'.
Re^6: Simple Matching
by ikegami (Patriarch) on Sep 28, 2009 at 15:53 UTC
    (?>[^$suits]) is the same as [^$suits]. The + should be inside the (?>).

    Captures are slow, so it would be faster to build a regex without captures I also generalised some of the matching. Input validation is not a goal here.

    m{ ^ . (?: D (?: . .D ){4} | S (?: . .S ){4} | C (?: . .C ){4} | H (?: . .H ){4} ) \z }xs;

    Or maybe even

    m{ ^ . (?: D..D..D..D..D | S..S..S..S..S | C..C..C..C..C | H..H..H..H..H ) \z }xs;
    built it manually here, but it can be built dynamically.

    Update: That last regex pattern gives me an idea:

    my $masked = $_ & "\x00\xFF \x00\xFF \x00\xFF \x00\xFF \x00\xFF"; ( $masked eq "\x00D \x00D \x00D \x00D \x00D" || $masked eq "\x00S \x00S \x00S \x00S \x00S" || $masked eq "\x00C \x00C \x00C \x00C \x00C" || $masked eq "\x00H \x00H \x00H \x00H \x00H" )

    Of course, if the input format was a number, it would be even faster:

    sub build { return ( $_[0] << (16*4 + 2*5 ) ) + ( $_[1] << (16*4 + 2*4 ) ) + ( $_[2] << (16*3 + 2*4 ) ) + ( $_[3] << (16*3 + 2*3 ) ) + ( $_[4] << (16*2 + 2*3 ) ) + ( $_[5] << (16*2 + 2*2 ) ) + ( $_[6] << (16*1 + 2*2 ) ) + ( $_[7] << (16*1 + 2*1 ) ) + ( $_[8] << (16*0 + 2*1 ) ) + ( $_[9] << (16*0 + 2*0 ) ); } use constant { SUITS_MASK => build(0,3, 0,3, 0,3, 0,3, 0,3), ALL_D => build(0,0, 0,0, 0,0, 0,0, 0,0), ALL_S => build(0,1, 0,1, 0,1, 0,1, 0,1), ALL_C => build(0,2, 0,2, 0,2, 0,2, 0,2), ALL_H => build(0,3, 0,3, 0,3, 0,3, 0,3), }; my $suits = $_ & SUITS_MASK; ( $suits == ALL_D || $suits == ALL_S || $suits == ALL_C || $suits == ALL_H )