Hello again, I have several lines of data that I am reading into a hash, as per the code below. I would like to print out only those lines where the key is duplicated. The key, in this case, is the data in the first TWO columns on each line (for example '310715 W'):
What I would like to see printed is just the following line which have the same key (i.e. 314303 X):
Can anyone suggest a way of achieving this?
I have a working script to do this using an array (lines are compared in pairs) but I am sure that a hash solution must be more efficient?
Code:
#!/usr/bin/perl
use strict;
my %file_hash;
while (<DATA>) {
chomp;
next unless $_ =~ m/^\d/;
my ($key, $status, $value) = split(/\s+/,$_,3);
$file_hash{"$key\t$status"} = $value;
}
foreach my $key ( keys %file_hash ) {
print "$key -- $file_hash{$key}\n";
}
__DATA__
301625 W 322500 1 07/31/2009
305671 C 155900 1 07/31/2009
306526 W 69900 1 07/31/2009
308064 W 895000 1 07/31/2009
308548 H 89000 0 07/31/2009
309245 H 88000 1 07/31/2009
310708 W 199900 1 07/31/2009
310715 W 199900 0 07/31/2009
311018 H 142500 0 07/31/2009
311018 W 137900 0 07/31/2009
312911 H 53900 0 07/31/2009
313984 W 554900 1 07/31/2009
314303 X 47000 0 07/31/2009
314303 X 69300 0 07/31/2009
314303 X 69900 0 09/21/2009
314306 W 146000 0 07/31/2009
314389 H 90000 0 07/31/2009
315309 W 149900 0 07/31/2009
315671 W 150000 1 07/31/2009
What I would like to see printed is just the following line which have the same key (i.e. 314303 X):
Code:
314303 X 47000 0 07/31/2009
314303 X 69300 0 07/31/2009
314303 X 69900 0 09/21/2009
Can anyone suggest a way of achieving this?
I have a working script to do this using an array (lines are compared in pairs) but I am sure that a hash solution must be more efficient?