in reply to Positional regex statements

Here is one of my favorite tricks using substr's lvalue property.

for my $spot (reverse 11,25,31,35,39,44,49,66,71,76,78) { substr($_, $spot) =~ s/^\s*/,/; }
The list of positions is reversed to make the substitutions work from right to left, so that the positions remain valid as the substitutions are made. That substitution and regex will non-destructively insert a comma in the given position even if there is no whitespace there, and will also eat leading whitespace from that position if it exists.

After Compline,
Zaxo

Replies are listed 'Best First'.
Re^2: Positional regex statements
by jcoxen (Deacon) on Oct 05, 2004 at 14:05 UTC
    Very nice. My only problem is that, on closer examination, I found that the data dump truncates white space off the end of short lines, so substr ends up complaining that some of the substitutions are out of range. I'll need to use a series of regex statements or a foreach loop to handle this one.

    Thanks for the suggestion, though. I'm gonna squirrel that one away for future use.

    Jack