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

In reply to Trying to remove duplicate rows using hashes by Angharad

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.