in reply to perl writing to xml file (xml::writer)

Remember the name of the structure in a variable, start a new structure only when the current name is different.

I also fixed the code in order to be able to run it with the data you showed (no commas, no root node) and without creating any files. Adapt to your desires as needed.

#!/usr/bin/perl use warnings; use strict; use XML::Writer; my @array = split /\n/, << '__CSV__'; name subname start size CAL_M reserved 0 13 CAL_M o_check_out 13 7 CAL_M o_ttr_first 20 4 CAL_M o_cal_check_1 24 4 PSQN_0 reserved 0 8 PSQN_0 check_final 8 10 PSQN_0 o_check 18 10 __CSV__ my $writer = 'XML::Writer'->new( DATA_MODE => 1, DATA_INDENT => 2); $writer->startTag('root'); my $prev_name = q(); for my $item(@array) { chomp $item; if (my ($name, $sname, $start, $size) = $item =~/(\w+)\s+(\w+)\s+( +\d+)\s+(\d+)/) { if ($prev_name ne $name) { if ($prev_name) { $writer->endTag('structure'); } $writer->startTag('structure', name => $name); } $writer->startTag('value', type => $size, name => $sname, start => $start, ); $writer->endTag('value'); $prev_name = $name; } } $writer->endTag($_) for qw( structure root ); $writer->end;
($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Replies are listed 'Best First'.
Re^2: perl writing to xml file (xml::writer)
by Anonymous Monk on Feb 03, 2016 at 06:25 UTC
    thanks choroba. btw, what does this actually do? my $prev_name = q(); is it the same as my $prev_name = ''; ?

      Yes, it is entirely equivalent. See perlop for the details.