in reply to Parsing Header Last Field

Dear Monks,
I would like to give a feedback.
It works great now (the hash solution twice so fast as a solution with a sub).
Still I have a problem :-)
There are many files which I process. Neither of files have every field in the header, which I modify. Some of files have even no such fields in their header. I noticed that e.g.
 $index{"NAME"}; returns undef in these cases which is interpreted as 0. Therefore the first column in these files is erroneously modified.
I did the following thing:
if ($index{"NAME"} =~/\d+/) { $parts[$index{"NAME"}] = ""; }
and this works. I need to repeat this preamble each time however. Is this a workaround or the solution (i.e. can this be done better)? Please note that the different fields are processed differently (some become "", some become replacement of digits etc.). Thanks! VE

Replies are listed 'Best First'.
Re^2: Parsing Header Last Field
by Marshall (Canon) on Jul 10, 2011 at 16:00 UTC
    Sounds like you need something like this:
    $parts[$index{Name}] =~ tr/A-Za-z/B-Wzb-w/ if defined ($index{Name});
    Checking whether or not the hash key is defined or exists is faster and more clear than running a regex to see if it contains a digit.

    Perhaps you need something similar to this on other replacements also?