in reply to Pattern Matching, left-to-right

My bad.

All I was shooting for was that if you want 'left to right' and you are having trouble getting something to work, I've often found that making explicitly 'left to right' (by putting it a while loop) can help me understand it. Once understanding has occurred, it can usually be re-written in a cooler way.

Replies are listed 'Best First'.
Re^2: Pattern Matching, left-to-right
by Aristotle (Chancellor) on Aug 21, 2004 at 13:31 UTC

    I don't know how 1 while s/// is clearer than s///g.

    What's worse, it does not work correctly:

    my %map = ( F => "|", S => "^", T => "&", R => "~", E => "\\" ); $_ = "\\E\\S\\"; s/\\([FSTRE])\\/$map{$1}/g; print "s///g: [$_]\n"; $_ = "\\E\\S\\"; 1 while s/\\([FSTRE])\\/$map{$1}/; print "1 while s///: [$_]\n"; __END__ s///g: [\S\] 1 while s///: [^]

    \E\S\ gets translated to \S\, as per the OP's spec, but the 1 while s/// solution then blithely goes on to translate that as well. Oops.

    (Note that there's a bunch of minor mistakes in your code. The last slash in your s/// is missing and (FSTRE) should be ([FSTRE]).)

    Makeshifts last the longest.