cms72
cms72

Reputation: 177

In an array of hashes, how to remove a value that is already present in another key?

How do I remove a value that is already present in another key?

For example:

I have a tabulated file, File_A:

a   1
b   4
c   1
a   2
b   5
c   6

and

#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dumper;

my %hash;

while (File_A) {
    push {@{$hash{$File_A[0]}, $File_A[1]}};
}

So that my %hash is equal to this
%hash = (
    a => [ '1','2'],
    b => [ '4','5'],
    c => [ '1','6'],
);

The value 1 is present in both keys a and c. Is there a way where I can prevent a duplicate value from being pushed into the array of hashes so that it looks like this?

%hash = (
   a => [ '1','2'],
   b => [ '4','5'],
   c => [ '6'],
);

Upvotes: 2

Views: 217

Answers (1)

zdim
zdim

Reputation: 66964

For each new value you need to check whether it's been seen. For this purpose all unique values should be recorded separately, to make it efficient and easy to check a new value against them.

Then use a hash to store seen values as keys, since it only takes a lookup to check whether a key exists. If it doesn't then process (add data to your results etc), and add that key to the hash with the ones already seen.

use warnings;
use strict;

use Data::Dump qw(dd);

my $file = shift || 'File_A';

my (%result, %seen);

open my $fh_A, '<', $file or die "Can't open $file: $!";

while (<$fh_A>) { 
    my ($key, $val) = split;

    next if exists $seen{$val};   # saw this value already, skip

    push @{$result{$key}}, $val;

    $seen{$val} = 1;
}

dd \%result;

I use Data::Dump to see complex data. If installing this is a problem use core Data::Dumper

Upvotes: 1

Related Questions