in reply to Re^2: Returning an array in a specific way for csv
in thread Returning an array in a specific way for csv
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"
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Returning an array in a specific way for csv
by Arengin (Novice) on Mar 14, 2017 at 08:25 UTC |