Hi All,

Want to convert txt file to xm,using xml::simple.

Hash of hash is not forming correctly. Can anyone please guide me to resolve, the root cause of mal-forming of hash.

use Data::Dumper; my $path; my @data; my $final = {}; while (<DATA>){ last if /^__END__$/; # stop when __END__ is encountered s/\n//gsi; s/=/./gsi; push (@data,$_); } foreach (@data){ print "$_ \n"; createHash($_); } print Dumper(\$final); sub createHash{ my $val = shift; my @x = split('\.',$val); my $i = 0; my $f = {}; while($i < scalar(@x)){ $f->{$x[$i+1]} = '['.$x[$i+2].']'; if(exists($final->{$x[$i]})){ print "second\n"; ($final->{$x[$i]}, $f); } else{ print "first\n"; $final->{$x[$i]} = ($final->{$x[$i]}, $f); } $i = $i+3; #print Dumper(\$final); } } use XML::Simple; $path = 'text2xml.xml'; open my $fh, '>:encoding(iso-8859-1)', $path or die "open($path): $!"; XMLout($final, OutputFile => $fh); __DATA__ a.b='xyz' a.c='abc' d='def' a.e='ghi' __END__ ## expected output <a> <b>xyz</b> <c>abc</c> <e>ghi</e> </a> <d>de</d>

Thanks in advance.


In reply to Ill-formed hash of hash - to convert text to xml by themonk

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.