Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Ovid (2709)

  (email not shown publicly)
AOL IM: ovidperl (Add Buddy, Send Message)

Stuff with the Perl Foundation. A couple of patches in the Perl core. A few CPAN modules. That about sums it up.

Journal of Ovid (2709)

Saturday January 15, 2005
01:42 AM

Fun with Permutations

[ #22721 ]

use Data::Dumper;
use re 'eval';
my $string = "abcd";
my $regex = qr//;
my @perms;
$regex .= qr/(\G[abcd]{0,4}(?{push @perms => [$&,$']}))/ for 1 .. 4;
$string =~ $regex;
print Dumper \@perms;

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • Took me some staring and blinking until I noticed what the regex was doing with the \G but no /g despite a loop. This is clearer:

    my $regex = qr/(\G[abcd]{0,4}(?{push @perms => [$&,$']}))/ x 4;

    It might be personal bias that I hate doing string concatenations in a loop, but this seems to be a case where the x operator is a natural fit.

    (Your final regex isn't precompiled, btw. I don't know if you are aware of that.)

    I'm not sure the output I'm seeing either way is intended, though? It simply list