Angharad has asked for the wisdom of the Perl Monks concerning the following question:
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).
Example file (tiny compared to the real thing).
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 thisd1 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
The print out should include all four columns Here is what I've attempted so fard1 c1.1 f1 d1.1 d1 c1.1 f2 d1.2 d5 c1.1 f4 d1.4 d6 c1.1 f5 d1.5
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#!/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"; }
|
---|
Back to
Seekers of Perl Wisdom