themonk has asked for the wisdom of the Perl Monks concerning the following question:
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Ill-formed hash of hash - to convert text to xml
by toolic (Bishop) on Jul 20, 2015 at 16:35 UTC | |
|
Re: Ill-formed hash of hash - to convert text to xml
by AnomalousMonk (Archbishop) on Jul 20, 2015 at 23:15 UTC | |
|
Re: Ill-formed hash of hash - to convert text to xml
by AnomalousMonk (Archbishop) on Jul 21, 2015 at 06:16 UTC | |
by themonk (Acolyte) on Jul 21, 2015 at 13:53 UTC | |
|
Re: Ill-formed hash of hash - to convert text to xml
by NetWallah (Canon) on Jul 21, 2015 at 06:58 UTC |