zock
zock

Reputation: 223

Perl How to print unique pairs from a file

I am trying to figure out how to print unique pairs of data from a file using Perl.

For instance,

Input: (check.pl)
A23072 A25058
A25058 A23072 
Output:
A23072 A25058

To tackle the task, I created a copy of the file check.pl and iterated through it to find the regular expression. I wrote the following code but I am unable to filter the data.

#!/usr/bin/perl -w
use strict;
use warnings;
open FH, "<check.txt" || die "Error\n";
open FH1, "<checkcopy.txt" || die "Error\n";
chomp (my @array=<FH1>);
my %count=();

while (<FH>) 
{
my @values = split;
next if grep /\D/, @values or @values != 2;

my $re = qr/\A$values[0]\s+$values[1]\z|\A$values[1]\s+$values[0]\z/;

   foreach my $key (@array) 
   {
      if ((grep $_ =~ $re, $key) && (grep ++$count{$_} == 1, $key) )
      {
        print "$key\n";
      }
   }    
}
1;

Any help will be appreciated! Thanks.

Upvotes: 0

Views: 292

Answers (2)

user507077
user507077

Reputation:

You could store the values in a hash as well which makes looking them up much easier. Something like:

my %duplicates;
while (<>) {
  my @values = split;
  next if @values != 2;

  my @sorted  = sort @values;
  $duplicates{ $sorted[0] } ||= {};
  next if $duplicates{ $sorted[0] }->{ $sorted[1] };

  $duplicates{ $sorted[0] }->{ $sorted[1] } = 1;
  print join(' ', @values), "\n";
}

Upvotes: 3

Chris Charley
Chris Charley

Reputation: 6573

Here is a technique I saw for a similiar problem using sort.

#!/usr/bin/perl
use strict;
use warnings;

my %seen;
my @array;

while (<DATA>) {
    next unless 2 == (@array = split);
    my $key = join "", sort @array;
    print unless $seen{$key}++;
}

__DATA__
A23072 A25058
A25058 A23072

Upvotes: 0

Related Questions