in reply to Writing hashes as records to a CSV file

G'day Serene Hacker,

It would have helped if you'd shown a sample of your actual input file. The two hashrefs you show give no indication of the data source or how the data shown was extracted; in fact, I'm not at all convinced that those hashrefs are necessary, even as temporary data structures.

Purely for demonstration purposes, let's assume your input file records are URLs; and the query string (which could be empty or even absent) represents the "containing key,value pairs".

$ cat pm_11139481_csv_kv_pairs_data.txt http://example.com/?AB1=101&NN=201&XYZ=401 http://example.net/?AB1=102&XYZ=402&MM=302 http://example.org/?MM=303&XYZ=403&NN=203&AB1=103 http://example.mil/? https://example.mil/ http://example.edu/?PQR=504

Note that I used unique values throughout. The last digit of each matches the record number. This facilitates quickly checking the output by visual inspection. (Note: in your example, two instances of 100, and two of 400, do not allow this.)

Now, process that data with:

#!/usr/bin/env perl use strict; use warnings; use autodie; use Text::CSV; my $source_data = 'pm_11139481_csv_kv_pairs_data.txt'; my $created_csv = 'pm_11139481_csv_kv_pairs_data.csv'; my %data; my $last_index = -1; extract_data($source_data, \%data, \$last_index); { my @headers = sort keys %data; my $csv = Text::CSV::->new(); open my $csv_fh, '>', $created_csv; $csv->say($csv_fh, \@headers); for my $i (0 .. $last_index) { $csv->say($csv_fh, [map $data{$_}[$i], @headers]); } } sub extract_data { my ($in_file, $data, $last_index) = @_; open my $fh, '<', $in_file; while (<$fh>) { ++$$last_index; chomp; my $qs = (split /[?]/)[1]; next unless defined $qs; for my $kv_pair (split /[&]/, $qs) { my ($key, $val) = split /[=]/, $kv_pair; $data->{$key}[$$last_index] = $val; } } return; }

Obviously, you'd need to write your own version of extract_data(); however, the rest of the code should probably work pretty much as is.

Here's the result:

$ cat pm_11139481_csv_kv_pairs_data.csv AB1,MM,NN,PQR,XYZ 101,,201,,401 102,302,,,402 103,303,203,,403 ,,,, ,,,, ,,,504,

— Ken