Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Ill-formed hash of hash - to convert text to xml

by NetWallah (Canon)
on Jul 21, 2015 at 06:58 UTC ( [id://1135541]=note: print w/replies, xml ) Need Help??


in reply to Ill-formed hash of hash - to convert text to xml

Try this alternative code:
use strict; use warnings; use Data::Dumper; my $hash; while (<DATA>){ my ($left,$val) = split /=/,$_ or next; my ($first,@levels) = split /\./,$left; my $str="\$hash->{$first}"; for my $last_level(@levels){ $str .= "{$last_level}" } eval "$str=$val"; } print Dumper \$hash; __DATA__ a.b='xyz' a.c='abc' d='def' a.e='ghi'
If using for production, you may want to sanitize the data before the string eval.
Output:
$VAR1 = \{ 'a' => { 'e' => 'ghi', 'c' => 'abc', 'b' => 'xyz' }, 'd' => 'def' };

        "Despite my privileged upbringing, I'm actually quite well-balanced. I have a chip on both shoulders."         - John Nash

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1135541]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-04-25 07:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found