utku has asked for the wisdom of the Perl Monks concerning the following question:
What I want to obtain is:$fc = '#4 r0 ! r1 ! r2 ! #5 r3 ! r4 ! r5 ! ';
.. which means that foreach (#\d+) and ((\S+\s+\S+\s+)*) match, I need to take first item and then last item of the 2nd and nested match. Following code works:#4 r2 ! #5 r5 !
.. but what I don't like is that I need to store the new string into a second var like "$ofc". Is there a way to remedy 2nd var and thus to reduce this loop into one single substitution?: $fc =~ s/(#\d+)\s+((\S+\s+\S+\s+)*)/$1$&/g; ... which doesn't do the job I want. Holy monks, do I want too much? Could substitue s///g be faster than global matching for this case?while ($fc =~ /#(\d+)\s+(\S+\s+\S+\s+)*)/g ) { $time = $1; $list = $2; @rlist = split /\n/, $list; $ofc .= "#$time\n$rlist[$#rlist]\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: substitute instead global matching for nested matches
by GrandFather (Saint) on Mar 18, 2012 at 22:51 UTC | |
by utku (Acolyte) on Mar 18, 2012 at 23:06 UTC | |
by GrandFather (Saint) on Mar 18, 2012 at 23:19 UTC | |
by utku (Acolyte) on Mar 18, 2012 at 23:27 UTC | |
|
Re: substitute instead global matching for nested matches
by wrog (Friar) on Mar 18, 2012 at 23:30 UTC | |
by utku (Acolyte) on Mar 20, 2012 at 13:56 UTC |