Since you asked for pointers, I recoded your routine below. One major thing is that my code contains fewer levels of indentation and a lot fewer "if" statements.

I presume that you will make some sort of subroutine that might update multiple lines at once. Here I will just concentrate on the parts that you have shown.

Get your "enddate" and "state" "ducks in order" before you start looping. In other words, don't bury some data validation stuff (of your sub's input data) deep in the guts of what it is doing. Do it early so that you KNOW that your new data is valid.

Below I used the ||= operator. This tests "truthfulness" and if a variable doesn't evaluate to "true" then you get what is on the right hand side. Probably here the not so well known //= operator would be better, this tests for "definedness" instead of "truthness". So whatever you give me is ok, unless it is "undefined". Your $x = xyz unless $x is just fine also. I am just showing other ways to do that.

In the main loop, I use a regex to decide if this is a line that I'm really interested in or not? I want to make that decision easily and quit further processing. Using a regex here: 1) is faster than splitting into all the component parts of the data record, 2) allows for comment (# lines) or 3) blank lines to just be skipped. Often files that are edited by humans or generated with "copy-n-paste" can have a trailing blank line that causes some error message.

Once it is clear that we are one the "right" line, only "straightline" code is used, no if's or branches. We know for example that $enddate is an valid value because all that was done before we got here!

I would steer you away from cryptic, seldom used things like local $^I = ".bak";. Explicitly open an output file and use that filehandle for printing the result.

#!/usr/bin/perl -w use strict; my $requestid = 3; my $enddate = 'Aug 13 2010 9:30AM'; #field 9 my $state = 'failed'; #field 10 $requestid ||= 0; #or possibly use the //= operator $enddate ||= "none"; $state ||= "none"; while (<DATA>) { unless (/^$requestid,/) { print; next;} #if (!/^$requestid,/) { print; next;} #same thing if you want chomp; my @fields = split (/,/,$_); $fields[10] = $enddate; $fields[11] = $state; print join(",",@fields), "\n"; } =prints for line 3 ... others are unchanged 3,AB499,none,Somebody.Admin@mysite.com,none,none,1,dbase,none,none,Aug + 13 2010 9:30AM,failed =cut __DATA__ 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

In reply to Re: Problem with inline replace by Marshall
in thread 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.