in reply to Finding names in files

Perl keeps track of the current line number for you in the $. variable, so no need to count them your self.

Here's an implementation you can learn from (hopefully :-):

for my $file (@files) { my $has_dave; open my $fh, "<", $file or warn("Can't open $file - $!\n"), next; while (<$fh>) { last if $. > 20; $has_dave = 1 if /dave/i; } print "$file\n" unless $has_dave; }

Replies are listed 'Best First'.
Re^2: Finding names in files
by gzayzay (Sexton) on Mar 29, 2006 at 18:23 UTC
    Thanks duff,

    However, I do not understand the following part of your code.

    for my $file (@files)

    Why are You using $file(@files), could you kindly explain that to me.



      That syntax puts the default variable (i.e., the array element that he's working on) into $file so he can use that in his loop instead of $_ or $file=$_;

      No muss, no fuss