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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |