in reply to Re: perl: How to comapre and manipulate csv file in perl?
in thread perl: How to comapre and manipulate csv file in perl?

if(!exists $result{$uniqueKey}) # If this combinaison is unknown { $result{$uniqueKey} = [\%currentRow]; # save the current row to i +t } else { push @{ $result{$uniqueKey} }, \%currentRow; # add the current ro +w to the existing ones }

The exists test is not needed because autovivification (see perlglossary) will create an empty anonymous array reference to push a new value into in the case of a key that does not already exist:

c:\@Work\Perl\monks>perl -wMstrict -le "use Data::Dump; ;; my @ra = qw(foo a bar w bar x foo b bar y); dd \@ra; ;; my %hash; while (@ra >= 2) { my $k = shift @ra; my $v = shift @ra; ;; push @{ $hash{$k} }, $v } dd \%hash; ;; @ra = qw(foo A bar W bar X foo B bar Y); dd \@ra; ;; my $hashref; while (@ra >= 2) { my ($k, $v) = splice @ra, 0, 2; push @{ $hashref->{$k} }, $v; } dd $hashref; " ["foo", "a", "bar", "w", "bar", "x", "foo", "b", "bar", "y"] { bar => ["w", "x", "y"], foo => ["a", "b"] } ["foo", "A", "bar", "W", "bar", "X", "foo", "B", "bar", "Y"] { bar => ["W", "X", "Y"], foo => ["A", "B"] }

Replies are listed 'Best First'.
Re^3: perl: How to comapre and manipulate csv file in perl?
by Eily (Monsignor) on Oct 01, 2014 at 16:45 UTC

    Oh, right! I keep forgetting that autovivification isn't just when you do something like $name{KEY}[0][1].