Hi, I'm having a problem when trying to use inline replace to substitute a couple of fields in a file. An example file would be
1,AB499,Joe.Bloggs@mysite.com,MY_SERVER_ENV,sales,/opt/backup/MY_SERVE +R_ENV,sales.data,1,dbase,Apr 25 2008 3:25PM,Apr 25 2008 3:30PM,comple +ted 2,AB499,none,Somebody.Admin@mysite.com,none,none,1,dbase,none,none,non +e 3,AB499,none,Somebody.Admin@mysite.com,none,none,1,dbase,none,none,non +e 4,AB499,none,Somebody.Admin@mysite.com,none,none,1,dbase,none,none,non +e 5,AB499,none,Somebody.Admin@mysite.com,none,none,1,dbase,none,none,non +e 6,AB499,none,Somebody.Admin@mysite.com,none,none,1,dbase,none,none,non +e 7,XX777,myserver,Anon.Person@mysite.com,business,/nfs/busback/upload/i +ncident,10,dbase,none,none,none 8,XX777,myserver,Anon.Person@mysite.com,business,/nfs/busback/upload/i +ncident,10,dbase,Aug 13 2010 8:30AM,Aug 13 2010 9:00AM,completed
What I'm trying to do is to replace the contents of fields 10 and 11 with those passed to the perl utility but only for the line which matches the id (the first field) whilst maintaining the rest of the record as is and the other records in the file as is.

Here is a snippet from the code .

$requestid = 0 unless $requestid; $enddate = "none" unless $enddate; $state = "none" unless $state; if ($update) { local $^I = ".bak"; # in place editing with backup fil +e @ARGV = qw(/test/info.dat); while (<>) { chomp; my @array = split /,/,$_; if ($array[0] eq $requestid) { if ($enddate) { s/$array[9]/$enddate/g + ;} if ($state) { s/$array[10]/$state/g ;} print; print "\n" ; } else { print join(",",@array); print "\n"; } } }
Now the problem. An example is if I pass the utility the enddate (Aug 13 2010 9:30AM) and state (failed) for a requestid of 3 it's doing this to the file.
1,AB499,Joe.Bloggs@mysite.com,MY_SERVER_ENV,sales,/opt/backup/MY_SERVE +R_ENV,sales.data,1,dbase,Apr 25 2008 3:25PM,Apr 25 2 008 3:30PM,completed 2,AB499,none,Somebody.Admin@mysite.com,none,none,1,dbase,none,none,non +e 3,AB499,Aug 13 2010 9:30AM,Somebody.Admin@mysite.com,Aug 13 2010 9:30A +M,Aug 13 2010 9:30AM,1,dbase,Aug 13 2010 9:30AM,Aug 13 2010 9:30AM,Aug 13 2010 9:30AM 4,AB499,none,Somebody.Admin@mysite.com,none,none,1,dbase,none,none,non +e 5,AB499,none,Somebody.Admin@mysite.com,none,none,1,dbase,none,none,non +e 6,AB499,none,Somebody.Admin@mysite.com,none,none,1,dbase,none,none,non +e 7,XX777,myserver,Anon.Person@mysite.com,business,/nfs/busback/upload/i +ncident,10,dbase,none,none,none 8,XX777,myserver,Anon.Person@mysite.com,business,/nfs/busback/upload/i +ncident,10,dbase,Aug 13 2010 8:30AM,Aug 13 2010 9:00 AM,completed
If I then run the utility again using the same params it then produces this the next time around.
1,AB499,Joe.Bloggs@mysite.com,MY_SERVER_ENV,sales,/opt/backup/MY_SERVE +R_ENV,sales.data,1,dbase,Apr 25 2008 3:25PM,Apr 25 2 008 3:30PM,completed 2,AB499,none,Somebody.Admin@mysite.com,none,none,1,dbase,none,none,non +e 3,AB499,failed,Somebody.Admin@mysite.com,failed,failed,1,dbase,failed, +failed,failed 4,AB499,none,Somebody.Admin@mysite.com,none,none,1,dbase,none,none,non +e 5,AB499,none,Somebody.Admin@mysite.com,none,none,1,dbase,none,none,non +e 6,AB499,none,Somebody.Admin@mysite.com,none,none,1,dbase,none,none,non +e 7,XX777,myserver,Anon.Person@mysite.com,business,/nfs/busback/upload/i +ncident,10,dbase,none,none,none 8,XX777,myserver,Anon.Person@mysite.com,business,/nfs/busback/upload/i +ncident,10,dbase,Aug 13 2010 8:30AM,Aug 13 2010 9:00 AM,completed
The default for most of the fields is none. The substitute command is obviously not working the way I wanted it to.I have no idea what is happening here . I'd be grateful for any help or pointers to writing better perl code !

In reply to Problem with inline replace by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.