On May 10, Tucker, Ernie said:

>Here is my first problem to the list. I have two different files with
>information that I need to find out what both have.

This is answered in the Perl FAQ:

  japhy% perldoc -q intersection
  Found in /usr/local/lib/perl5/5.00502/pod/perlfaq4.pod

  "How do I compute the difference of two arrays?  How do I compute the
  intersection of two arrays?"

  Use a hash.  Here's code to do both and more.  It assumes that
  each element is unique in a given array:

    @union = @intersection = @difference = ();
    %count = ();
    foreach $element (@array1, @array2) { $count{$element}++ }
    foreach $element (keys %count) {
      push @union, $element;
      push @{ $count{$element} > 1 ? \@intersection : \@difference },
        $element;
    }

This assumes you have two arrays, which you don't, technically.  However,
you can make the following adjustments:

  open FILE_1, "< this" or die "can't read this: $!";
  $seen{$_}++ while <FILE_1>;
  close FILE_1;

  open FILE_2, "< that" or die "can't read that: $!";
  while (<FILE_2>) {
    print if $seen{$_};
  }
  close FILE_2;

This is better than plopping the content of the files into arrays and
using the code from the FAQ.

-- 
Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
<stu> what does y/// stand for?  <tenderpuss> why, yansliterate of course.
[  I'm looking for programming work.  If you like my work, let me know.  ]


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to