File2 is a subset(w/ some repetitions) of the second column from file1. I need to print the entire line from file1 that does NOT contain the rows in file2
I also question why you would want to use regex's, even if the file doesn't have fixed width fields and only appears that way to make your post look pretty. If file 2 stores the whole value of column 2, why not just store the values of file 2 in a hash? Then when you read file 1, split the row into fields, and look up the value of field 2 in a hash to see if you want the line.
my @aKeep; while (my $line = <F1>) { # split line into fields - depends on how the line is formatted @aFields = .... if (!exists($hValuesInColumn2{$aFields[1]}) { push @aKeep, $line; } }
A few efficiency comments as well. There is no need to explicitly "unique" the second file if you use a hash to store the field values. Since a hash key is stored only once it automatically "uniques" the keys.
Also when reading in the lines of a file, it is better to use while than foreach. foreach slurps in all of the lines into an array and then visits each, whereas while reads the lines in one at a time and is much more memory efficient.
my %hValuesInColumn2; # use while, not foreach while (my $line = <F2>) { #get rid of trailing new line chomp $line; # strip remaining leading and trailing whitespace # (if that is an issue) $line =~ s/^\s+//; #leading $line =~ s/\s+$//; #trailing # record field value $hValuesInColumn2->{$line} = 1; } # extracting the keys may not even be necessary # see first part of this answer. my @unique_f = keys %hValuesInColumn2;
Update: reordered paragraphs to put focus on OP's original question rather than efficiency issues.
In reply to Re: comparing columns using regular expression
by ELISHEVA
in thread comparing columns using regular expression
by rocky13
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |