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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.