Honza Mach wrote:
Hi everybody,
Hello,
I was wondering, if it is possible to use backreferences in the pattern repetition bracket operator. Consider the following string: my $string = "5 abcdefghijklmn"; The number five at the beginning of the string means, that I want to extract first five characters from the latter part of the string. I tried the following code, but it doesn`t work: $string =~ s/(\d+)\s+(.{\g1})//; print "extracted: $1 $2\n"; The desired output would be: extracted: 5 abcde It seems, that it is not possible to use backreferences within the bracket operator (or am I doing something wrong?). Is there other solution to my problem.
If it always involves a single digit followed by a single space you could use unpack()
$ perl -le' my $string = "5 abcdefghijklmn"; my ( $num, $extract ) = unpack "A2 X2 A2/a*", $string; print "$num $extract"; ' 5 abcde John -- Any intelligent fool can make things bigger and more complex... It takes a touch of genius - and a lot of courage to move in the opposite direction. -- Albert Einstein -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/