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

Hi, I am trying to remove lines from a file when i match a value in each line of a file against numbers stored in an array
while (<IN>) { chomp; /^\s+(\S+)/; push @array, $1; } close (IN); foreach $line (@array) { chomp; print "$line\n"; foreach $line2 (<IN2>) { $line2 =~/^\s+\S+\s+\S+\s+(\S+)/; if ($line == $1) {remove line}; } }
however it only appears to match the first value in the array and nothing else, when it should. I have checked the array to make sure values are stored

Replies are listed 'Best First'.
Re: remove a line
by Jaap (Curate) on Nov 22, 2004 at 10:07 UTC
    Well, let me 1st say something about speed. This thing is gonna be slow. Perhaps you could try something like this:
    my %match; if (open (FH1, "first.txt")) { while (<FH1>) { if (/^\s+(\S+)/) { $match{$1} = 1; print "Found $1\n"; } } close (FH1); } if (open (FH2, "data.txt")) { while (<FH2>) { if (/^\s+\S+\s+\S+\s+(\S+)/) { if ($match{$1}) { print "Match on $1\n"; } else { print "NO match on $1\n"; } } } close (FH2); }
    code is untested.
      thanks, seems to have done the job