in reply to Problem with inline replace

You are using the regex substitution operator the wrong way at a place where a simple assignment is sufficient. Change to this:

if ($array[0] eq $requestid) { if ($enddate) { $array[9]= $enddate;} if ($state) { $array[10]=$state;} } print join(",",@array); print "\n";

PS: The following does the same but is probably faster (and might be what you intented with your version):

if ($array[0] eq $requestid) { if ($enddate) { $array[9]= $enddate;} if ($state) { $array[10]=$state;} print join(",",@array); print "\n"; } else { print; print "\n"; }

Replies are listed 'Best First'.
Re^2: Problem with inline replace
by Marshall (Canon) on Aug 15, 2010 at 06:45 UTC
    Your observation that assignment rather than using substitution is exactly on track! Great job!

    As far as performance goes, my experience is that split is an "expensive thing" CPU wise. A regex that tests whether a line starts with X is a relatively inexpensive thing. I have also found that the join() operation is very performant - i.e. joining 12 things together is way faster than splitting one thing into 12 things. Anyway I think the performance enhancement here is to not do a split at all unless you have to - my code is shown below which uses a regex to decide whether to split or not split.

Re^2: Problem with inline replace
by Anonymous Monk on Aug 13, 2010 at 13:39 UTC
    This is great thanks. When you say the p.s. version is faster can you elaborate ?

      It might be faster because the join function is only called on the one line where you want to change something. So join is called once for the file instead of once for each line in the file

      But you might not notice anything of the speedup because your CPU is usually waiting for your hard disc when writing a file. So it does the join while waiting anyway. But at least the CPU will be cooler with the PS version then

      Note, the differences I am talking about are minimal. If you don't have to write files in the gigabytes, there won't be any noticable changes, your script would be just a bit closer to perfection ;-)