I have a file for which I need to remove some rows for which there is an element of duplication.
Example file (tiny compared to the real thing).
d1 c1.1 f1 d1.1
d1 c1.1 f2 d1.2
d2 c1.1 f1 d1.1
d3 c1.1 f1 d1.1
d4 c1.1 f1 d1.1
d4 c1.1 f2 d1.2
d5 c1.1 f4 d1.4
d6 c1.1 f5 d1.5
For each c1.1 group, I want a print out whereby for each c1.? all duplicate d1.? entries are removed. In other words I'm after something like this
d1 c1.1 f1 d1.1
d1 c1.1 f2 d1.2
d5 c1.1 f4 d1.4
d6 c1.1 f5 d1.5
The print out should include all four columns
Here is what I've attempted so far
#!/usr/bin/perl -w
use strict;
use warnings;
use English;
use FileHandle;
use Exception;
my ($fIn) = $ARGV[0];
open(FILE, "$fIn") || die "ERROR: Can't open $fIn file: $!\n";
my %hash;
my $c_id;
my $d_id;
my $f_var;
while(<FILE>)
{
chomp;
my @data = split(/\s+/, $_);
$c_id = $data[1];
$d_id = $data[3];
$f_var = $data[2];
if(!$hash{$c_id}{$f_var})
{
$hash{$c_id}{$f_var} = $d_id;
}
}
while (( my $k1, my $k2) = each %hash)
{
print "$k1 ";
while (( $k2, my $k3) = each %$k2)
{
print "$k2 $k3 ";
}
print "\n";
}
But sadly I'm getting an error about not being able to use a string as a HASH ref while 'strict refs' are in use.
Could someone please point me in the right direction? Thanks
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.