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,

In reply to Re: perl writing to xml file (xml::writer) by choroba
in thread perl writing to xml file (xml::writer) by analys

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.