Serene Hacker has asked for the wisdom of the Perl Monks concerning the following question:
I'd like to create a CSV file containing one row for each record.
However, each record doesn't necessarily contain exactly the same keys as the other records. So I'd like the column-headers to be a superset of all the keys. E.g.
-->{AB1 => 100, NN => 200, XYZ => 400} {AB1 => 100, XYZ => 400, MM => 300}
ie."AB1","NN","XYZ","MM" "100","200","400","" "100","","400","300"
I could do this "manually" as it were, but can one do it directly (using Text::CSV) ie. without figuring out the superset of keys myself? In Text::CSV I see one can *read* tables with headers as hashes. But can one write them just as easily?AB1 NN XYZ MM 100 200 400 undef 100 undef 400 300
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Writing hashes as records to a CSV file
by choroba (Cardinal) on Dec 08, 2021 at 23:24 UTC | |
|
Re: Writing hashes as records to a CSV file
by Tux (Canon) on Dec 09, 2021 at 08:55 UTC | |
|
Re: Writing hashes as records to a CSV file
by LanX (Saint) on Dec 08, 2021 at 23:27 UTC | |
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 | |
|
Re: Writing hashes as records to a CSV file
by kcott (Archbishop) on Dec 09, 2021 at 06:48 UTC |