On Aug 25, 7:01 am, jwkr...@shaw.ca ("John W. Krahn") wrote: > 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 >
Or, eliminating the single digit requirement with split/substr: ( $num, $ss ) = split " ", $string; print "$num ", substr $ss, 0 , $num; -- Charles DeRykus -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/