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


In reply to Re: Writing hashes as records to a CSV file by kcott
in thread Writing hashes as records to a CSV file by Serene Hacker

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.