in reply to string replacement question

A position-based string can easily take substitutions using substr. Since you presumably know the positions and widths, you can say:

while (<$ifh>) { substr($_, $position, $width) =~ s/foo/bar/; print $ofh $_; # update $position and $width if necessary }

Another approach is to unpack into an array or list of variables, make the modifications on those, and then pack back into a line to be printed to the output file.

Also, you can read a fixed-length chunk by setting local $/ = \42; (or whatever the record length is). That can be used to skip headers before the rest of the file is processed, too.

Update: Looking at the now-formatted data, I think I may have misunderstood what you meant by "position". Is this a CSV file? If so, follow that link.

After Compline,
Zaxo

Replies are listed 'Best First'.
Re^2: string replacement question
by edwardt_tril (Sexton) on Nov 30, 2005 at 07:17 UTC
    Hi I ain't sure about using width because the whole line content is read from a file, so it varies.
    I look up both pack/unpack and they seem to deal with ascii?
    not quite sure the pack/unpack for hiascii or dbcs case? can you give an example? thankx

      I was treating your data as fixed-width fields, confused by the unformatted version I guess.

      Can you usually get useful arrays out with split ',' ?. If so, follow the CSV search for lots of possibilities. The data looks like CSV now that it's laid out.

      Pack and unpack have several formats for dealing with ascii data - very useful for fixed width fields, but probably not what you need for this file.

      After Compline,
      Zaxo