in reply to match-time pattern interpolation

I believe japhy did this first. No doubt his solution is better than mine. But heres what I did:
my $str="1 2 3 10 6 7 8 11 12 13 14 15 161"; print "Before : $str\n"; # we use qr to avoid the use re 'eval'; my $rex=qr/(\d+) # match a number put it in $1 (?{$num=$1}) # set $num to be $1 (?: # dont capture all of this, \s+ # must have space between the n +umbers ( # capture into buffer $2 (??{'\b'.++$num.'\b'}) # match only the successor of +$num ) # end of buffer )+ # one or more /x; $str=~s/$rex/$1-$2/g; # and now substitute in print "After : $str\n";
Outputs
Before : 1 2 3 10 6 7 8 11 12 13 14 15 161 After : 1-3 10 6-8 11-15 161
Update: Which really isnt that different to yours (which I actually didnt look at too closely until after I had done mine. Kinda funny :-)

HTH

--- demerphq
my friends call me, usually because I'm late....

Replies are listed 'Best First'.
Re: Re: match-time pattern interpolation
by perlguy (Deacon) on Nov 27, 2002 at 22:29 UTC
    looks like i was really, really close. i just needed a couple of \b's in my interpolation -- ((??{'\b' . ++$first . '\b'}))... thanks for the help!