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

hi. I have a program to readline by line from a file. I want to replace certain portion of the line then write it out to a new file.

but I don't know how to replace strings that contrain certain characters embeded within them: for example:

source string 1230,5,6,12344,adblock.dll,c:\program files\document and settings\1800cursor.dll,,,,,123,1,2,2 new string: 1230,5,6,12344,adblock.dll,c:\program files\system32\afvdefdcec.exel,,,,,123,1,2,2 another example: 12,3,4,ascii sample,123,c:\ascii\121243.php,1,3,45,5 to 12,3,4,hi-ascii sample,123,c:\@#$%^.rexehp,1,3,45,5

the only thing I know it ia position based. how can i do that? my goal is to make a function that takes (sourceline, source_word,replacement) then it will do the replacement regardless all these 3 parameters contain hiascii,ascii, or dbcs.. how to archieve that. I only have a version that works with ascii and without those special characters like \ ....

please help.

GrandFather fixed formatting

Replies are listed 'Best First'.
Re: string replacement question
by Zaxo (Archbishop) on Nov 30, 2005 at 06:31 UTC

    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

      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

Re: string replacement question
by serf (Chaplain) on Nov 30, 2005 at 06:48 UTC
    If you use <code> and </code> above and below your examples and <p> to break paragraphs your posts will be much easier for people to read.

    You can find these and other tags on the Writeup Formatting Tips node.

    You are able to go back and update your posts (including this one) if you wish to.