in reply to Writing hashes as records to a CSV file

You can collect the column names by iterating over the records and accumulating their keys.
#!/usr/bin/perl use warnings; use strict; use feature qw{ say }; use List::Util qw{ uniq }; use Text::CSV_XS; my @records = ({AB1 => 100, NN => 200, XYZ => 400}, {AB1 => 100, XYZ => 400, MM => 300}); my @columns = sort +uniq(map keys %$_, @records); my $csv = 'Text::CSV_XS'->new({auto_diag => 1}); for my $record ({map { $_ => $_ } @columns}, @records) { $csv->say(\*STDOUT, [@$record{@columns}]); }
map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]