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

Hello ,Monks!
I suppose it's an easy question,
however I'm stuck for some time now....
Here's my square code:
@ARGV=glob "exa*.txt"; $^I=".bak"; while (<>) { if ($.<15) { s/^.*$//; } else { print; } }
As you see I'm trying to delete first 15 lines from each file.
The problem is that it works only for the first file.
How can I make it work for all files? Thanks in advance!!!

Replies are listed 'Best First'.
Re: editing files with (<>)
by Aristotle (Chancellor) on Dec 21, 2004 at 08:03 UTC

    The simplest solution would be

    $^I = '.bak'; for my $file ( glob 'exa*.txt' ) { @ARGV = $file; while( <> ) { print if $. >= 15; } }

    Note that your s/// was superfluous, since if you don't print it, it doesn't end up in the file anyway. And even so, it was inefficient — a simple $_ = ""; would have sufficed.

    Makeshifts last the longest.

      $_="" is different from s/.*// because the regexp keeps the line end ("\n" or CR or CRLF or LF)

      $\=~s;s*.*;q^|D9JYJ^^qq^\//\\\///^;ex;print

        I skipped over that since I assumed $_ = "" is what he wanted — he is dropping those lines wholesale after all. I should have noted the fact that it was wrong…

        Makeshifts last the longest.

Re: editing files with (<>)
by Skeeve (Parson) on Dec 21, 2004 at 08:06 UTC
    The other answer is correct. Just a hint that s/^.*$//; can be (in most cases) easily replaced by $_="\n" and a second hint: Try to negate your "if" and see what happens if you drop the whole "else"-part.

    $\=~s;s*.*;q^|D9JYJ^^qq^\//\\\///^;ex;print
      Can it? I think that  /^.*$/ will match a string that does not contain a newline, because of the '^' and '$' anchors. If I'm right, and given the cold medicine I'm taking that is certainly no given, any line with a '\n' will not match the regular expression. As $_ is being read from a file, they all will contain a newline. I think. Ugh, more sudafed, please.

      That said, people are right in pointing out that that part of the if clause can be eliminated since it is changing data then doing nothing with it. If nothing else, it should speed up the code.

      --
      tbone1, YAPS (Yet Another Perl Schlub)
      And remember, if he succeeds, so what.
      - Chick McGee

Re: editing files with (<>)
by ysth (Canon) on Dec 21, 2004 at 08:03 UTC
    If you manually close ARGV when you get to eof of a particular file (see eof), $. restarts at 0. Another thing you can do is check when a new file is opened (@ARGV will have one less element, the filename stored in $ARGV will be changed.)

    By the way, there seems to be no reason for you to do the s///, since you aren't printing the line.

      That would all be useful advice if he were doing this as a -n oneliner. Since he's not, he can simply loop file by file as I outlined above. No need to complicate the solution overly.

      Makeshifts last the longest.

Re: editing files with (<>)
by runrig (Abbot) on Dec 21, 2004 at 07:50 UTC
    Check the eof documentation.