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"] }
In reply to Re^2: perl: How to comapre and manipulate csv file in perl?
by AnomalousMonk
in thread perl: How to comapre and manipulate csv file in perl?
by Ankur_kuls
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |