Many ways to do this. I'm going to read the text file into a data structure that mirrors the output, rather than mirroring what the input looks like. This way, I'm going to parse directly into the records. What I could have done was to read into a series of hashes instead. This just proves it's doable:

use strict; use Text::CSV; my @data; my %header; { my $idx = 0; my @single_row; sub eos { if (@single_row) { push @data, [ @single_row ]; @single_row = (); } } sub index_for { my $key = shift; unless (exists $header{$key}) { $header{$key} = $idx++; } $header{$key}; } while(<DATA>) { chomp; if (/^$/) { # end of section eos(); } else { my ($key, $value) = split /\s*:\s*/, $_, 2; $single_row[index_for($key)] = $value; } } eos(); } my $csv = Text::CSV->new(); my @h = sort { $header{$a} <=> $header{$b} } keys %header; $csv->combine(@h); print $csv->string(), $/; $csv->combine(@$_), print $csv->string(), $/ foreach @data; __END__ a: a1 b: b1 c: c1 a: a2 b: b2 c: c2 a: a3 b: b3 c: c3 a: a4 b: b4 c: c4 d: d1

In reply to Re: Record dump-style text --> CSV? by Tanktalus
in thread Record dump-style text --> CSV? by fallenmonkey

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.