jcoxen has asked for the wisdom of the Perl Monks concerning the following question:

Hello, Monks,

I have a nagging little (and probably ego-shatteringly easy) problem that I can't figure out or search out the answer to. I have several files that I have to convert from 80 column data dumps to csv format by replacing the spaces at certain columns with commas and then deleting the remaining spaces. If I were using sed, I'd use a statement like

sed -e "s/./,/11"
to put a comma at column 11 of every line - repeating for columns 25, 31,35, etc. My problem is that Perl won't accept that syntax and I can't find or figure out the comparable Perl syntax. Ideally, I'd be able to put all the replacements on one line with something like this:
s/./,/{11,25,31,35,39,44,49,66,71,76.78};
I could load the row into an array and tackle the problem that way but I feel that I should be able to do it via regex a lot easier. Can anyone tell me what I'm missing?

Thanks,

Jack

Replies are listed 'Best First'.
Re: Positional regex statements
by Eimi Metamorphoumai (Deacon) on Oct 04, 2004 at 18:15 UTC
    It sounds like you don't need a regexp at all, just
    substr($_, 10, 1)=',';
    (watch out for 0-based offsets!) If you really want a regexp solution, you could do something like
    s/^(.{10})./$1,/;
      Focusing a bit too narrowly, I guess.
      </blinders>
      Thanks,

      Jack

Re: Positional regex statements
by Zaxo (Archbishop) on Oct 05, 2004 at 05:17 UTC

    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

      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