Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I need to use $. on a range of lines. But these lines are separted by three rows of text. The file i am working with is nealry 100 lines long. Code i am using:
local $/ = "# input for "; while (<FILE1>) { ($hash, $input, $for, $aa) = split( /\s+/, $_) if /^\# \input\b.*/; if ($aa =~ /LEU\b/) {$. ==1 && s/ (.*)/$z3leu $z2leu $z1leu $1/} if ($aa =~ /ALA\b/) {$. ==4 && s/ (.*)/$z3leu $z2leu $z1leu $1/}
is it possible to set $. so that it will edit lines 1,4,7,10 etc

Replies are listed 'Best First'.
Re: $. Input line number
by Roy Johnson (Monsignor) on Apr 14, 2004 at 15:52 UTC
    if ($. % 3 == 1) {...}

    The PerlMonk tr/// Advocate
Re: $. Input line number
by Fletch (Bishop) on Apr 14, 2004 at 16:00 UTC

    Of course since you've diddled $/ $. no longer really represents lines (in the "string terminated by \n" sense), but rather records which may consist of several strings-terminated-by-\n's. You'd be better off splitting your record into an array and mucking however with the third element of that.

Re: $. Input line number
by davido (Cardinal) on Apr 14, 2004 at 16:49 UTC
    This seems closely related to another question posted yesterday. Perhaps you could follow-up with an explanation of what your data-set looks like, and what operation you're trying to perform on it, so that now that we've seen your code we might have a crack at finding an elegant solution (if one exists).

    Another thing, if your file is only 100 lines long, you know that it's not going to grow by much ever, and you're trying to perform operations on records, it might be helpful to use the Tie::File module along with the 'recsep' configuration option like this:

    tie my @array, 'Tie::File', $filename, recsep => '# input for ';

    This will cause individual records to be read into an array, and any modifications you perform on the array's elements will be echoed back into the file. That might simplify your script enough that the remainder of the script's file logic will wax trivial.

    Hope this helps!


    Dave