That doesn't answer my question as to what the output for the input I showed should look like.

#!/usr/bin/env perl use warnings; use strict; my @data; my %tags; my $cur_tag; while (<DATA>) { chomp; if (/^BEGIN_TAG/) { push @data, {} } elsif ( my ($tag,$line) = /^(X\S+)\s+(.+)$/ ) { $data[-1]{$tag} .= " NewRec " if exists $data[-1]{$tag}; $data[-1]{$tag} .= $line; $tags{$tag}++; $cur_tag = $tag; } elsif ($cur_tag) { $data[-1]{$cur_tag} .= " NewLine ".$_; } } use Text::CSV; my $csv = Text::CSV->new({binary=>1,auto_diag=>2,eol=>"\n", sep_char=>";",always_quote=>1,blank_is_undef=>1}); $csv->print(select, [sort keys %tags]); for my $row (@data) { my @vals = map { $row->{$_} } sort keys %tags; $csv->print(select, \@vals); } __DATA__ BEGIN_TAG X1 a b X2 one two X4 foo X1 c d X3 test BEGIN_TAG X1 e f g h X2 three four X4 bar quz baz X2 five six

Output

"X1";"X2";"X3";"X4" "a b NewRec c d";"one two";"test";"foo" "e f NewLine g h";"three four NewRec five NewLine six";;"bar NewLine q +uz baz"

In reply to Re^3: Returning an array in a specific way for csv by haukex
in thread Returning an array in a specific way for csv by Arengin

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.