in reply to Writing hashes as records to a CSV file
I searched thru the documentation of Text::CSV but couldn't find that. But I'm confident Tux as one of the maintainers will know.
> I could do this "manually" as it were,
Merging hashes is not complicated in Perl:
after
%H = (%H,%$_) for @AoH
you'll have the superset in
keys %H
More importantly you can keep control over the order of columns created. (like ordering by count)
Do you really want to leave that to Text::CSV, which most probably will be random?
Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery
OUTPUT:use strict; use warnings; use Data::Dump qw/pp dd/; my @AoH = ( {AB => 100, NN => 200, XYZ => 400}, {AB => 100, XYZ => 400, MM => 300}, { map { ("A$_" => $_) } "A".."C" } ); my %H; %H = (%H,%$_) for @AoH; dd keys %H; my %count; map { map { $count{$_}++ } keys %$_ } @AoH; dd sort { $count{$b} <=> $count{$a} || $a cmp $b } keys %count;
("XYZ", "NN", "MM", "AC", "AA", "AB") ("AB", "XYZ", "AA", "AC", "MM", "NN")
NB: "AB" is last in the first result, even that it appears in each record.
The second result however will order the columns first by count and then by alphabetic order.
YMMV...
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Writing hashes as records to a CSV file (joining keys with slices)
by LanX (Saint) on Dec 09, 2021 at 13:45 UTC | |
by Tux (Canon) on Dec 09, 2021 at 14:06 UTC | |
by choroba (Cardinal) on Dec 09, 2021 at 21:41 UTC | |
by Tux (Canon) on Dec 10, 2021 at 08:03 UTC | |
by LanX (Saint) on Dec 09, 2021 at 14:45 UTC |