Friday, August 21, 2009

collect unique records from csv file

Follow discussion about this post on http://tech.groups.yahoo.com/group/id-perl/message/1843

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

$Data::Dumper::Terse=1;
$Data::Dumper::Indent=0;

my $csv = Text::CSV->new({
binary => 1,
eol => "\n",
sep_char => ';'
});

#for more real app, use Getopt::Long
open my $fh, '<', $ARGV[0] or die $!;

my ( $hash_unique );

while(<$fh>) {
$csv->parse( $_ );
$hash_unique->{ Dumper( $csv->fields ) } = [ $csv->fields ];
}

close $fh;

open my $fh_phone, ">", 'result-unique.csv' or die $!;

$csv->print( $fh_phone, [ @{$hash_unique->{$_}} ] )
for keys %$hash_unique;

close $fh_phone;

No comments: