in reply to substitutions on a single line

Assuming you've got your while loop set up right and you're doing inline editing... and that the only thing you need is to perform an edit on the first line, you could modify your "if" statement like this:

$. == 1 && $aa =~ /LEU\b/ && s/ (.*)/$z31eu $z2leu $z1leu $1/;

That way, only when $. (the filehandle line counter) equals zero (the first line of the file) you will actually modify the line. The rest of your iterations through the fils you'll basically skip the code that performs the substitution.

Remember, of course, that you do have to write each line anew. The most common strategy is to open the input file, open a temporary output file, read line by line from the input file as you write line by line to the output file. You must do this for every line of the file. And on the first line (in your case) you'll also perform additional modifications to the data. Finally, you close both files, and rename the temp file to the name of your input file.

Update: Changed $. == 0 to $. == 1 to correct the error pointed out by halley.


Dave

Replies are listed 'Best First'.
Re: Re: substitutions on a single line
by halley (Prior) on Apr 13, 2004 at 18:10 UTC
    $. is 1-based. Before reading, it is undef. After reading a line, it is 1. Unless you assign to it, it is never actually 0, and although undef == 0, that's not what you want to write here.

    --
    [ e d @ h a l l e y . c c ]

Re: Re: substitutions on a single line
by Anonymous Monk on Apr 14, 2004 at 13:02 UTC
    i have been trying your suggestion but with no success. This is what i have, the while loop should read each individual line?:
    while (<FILE1>) { ($hash, $input, $for, $aa) = split( /\s+/, $_) if /^\# \input\b.*/; $. == 1 && $aa =~ /LEU\b/ && s/ (.*)/$z31eu $z2leu $z1leu $1/; print output "$_"; }
      For one thing I'm not sure what $z31eu, $z21eu, and $z11eu contain, and I also don't know that your regexen are actually matching what you expect them to.

      At any rate, I would start lacing your program's logic with prints to see which step fails, and then look at the contents of the suspect variables at that point to see why your logic is failing.


      Dave