simply seth has asked for the wisdom of the Perl Monks concerning the following question:

I would like to omit or skip duplicate combinations of $row->[1] and $row->[9]
Individually 1 and 9 can repeat but not in the same value pair. Here is what I have so far ...
use Text::CSV_XS; my $csv = Text::CSV_XS->new ({ binary => 1 }) or die "Cannot use CSV: ".Text::CSV->error_diag (); open my $fh, "<:encoding(utf8)", "swma_data.csv" or die "swma_data.csv: $!"; while (my $row = $csv->getline ($fh)) { #begin while loop chomp; ### I know that I need to process $row->[1] and $row->[9] ## But I Do not know how to proceed from here }
Thanks

Replies are listed 'Best First'.
Re: Unique with Text::CSV_XS
by ikegami (Patriarch) on Jan 08, 2010 at 18:33 UTC
    Hashes are great tools to weed out duplicates
    my %seen; while (my $row = $csv->getline($fh)) { next if $seen{ $row->[1] }{ $row->[9] }++; ... }

    This is based on code in perlfaq4

Re: Unique with Text::CSV_XS
by Tux (Canon) on Jan 08, 2010 at 18:34 UTC
    my %seen; while (my $row = $csv->getline ($fh)) { # WTF do you think chomp is doing here? $seen{pack "ll", $row->[1], $row->[9]}++ and next; # change ll to +reflect types # continue processing ... }

    Enjoy, Have FUN! H.Merijn

      For the OP's benefit, I'll make this explicit:

      chomp; basically removes from $_ a terminating string equivalent to $\; chomp $foo; does the same, but to $foo rather than $_. See perldoc -f chomp.

      However, you have no use for chomp here. You're getting "rows" of data which has been parsed for you and are now hashrefs, not strings!

        Just for the record, $row is an arraref, not a hashref.

        $ echo 1,a | perl -MDP -MText::CSV_XS -wle'DPeek(Text::CSV_XS->new->ge +tline(*ARGV))' \AV() $ echo 1,a | perl -MDP -MText::CSV_XS -we'DDumper(Text::CSV_XS->new->g +etline(*ARGV))' $VAR1 = [ '1', 'a' ];

        Enjoy, Have FUN! H.Merijn