On Sun, Apr 12, 2009 at 18:34, Andrew Fithian <[email protected]> wrote:
> Hello everyone,
> I have a program that needs to find straights in a hand of cards. The hand
> is a string with no whitespace sorted by the cards' ranks, eg "9d10cJhQsKd".
> How can I identify if that hand contains a straight with a single regex? Is
> that even possible?
> Is there a way to escape the regex and do addition on a backreference to get
> something like /(\d+)[cdhs]\1+1[cdhs]\1+2/?
> Thanks,
> -Andrew
>
It is easier to just build it:
#!/usr/bin/perl
use strict;
use warnings;
my @rank = qw/ 2 3 4 5 6 7 8 9 10 J Q K A /;
my @hands;
for my $i (0 .. 9) {
push @hands, join '', map { $_ . "[cdhs]" } @rank[$i .. $i+4];
}
my $re = join '|', @hands;
$re = qr/^$re$/;
for my $s (qw/ 9d10cJhQsKd 8c10cJhQsKd /) {
print "$s ", $s =~ /$re/ ? "is" : "isn't", " a straight\n";
}
--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/