Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: String to PERL data structure

by shmem (Chancellor)
on Jul 13, 2007 at 08:46 UTC ( [id://626401]=note: print w/replies, xml ) Need Help??


in reply to String to PERL data structure

Straight forward, quick and dirty. Usual caveats for string eval apply.
use Data::Dump::Streamer; my $string; while (<DATA>) { chomp; s/\r//; # append a comma to lines not ending in 'Array' or '(' # or being (other than containing whitespace) empty lines $_ .= ',' unless /(Array|\(|^\s*)$/; $string .= $_."\n"; } sub Array { my %h; while(@_) { my ($key) = @{shift(@_)}; $h {$key} = shift(@_); } \%h; } print $string,"\n"; my $ref = eval $string; Dump $ref; __DATA__ Array ( [0] => Array ( [attrs] => Array ( [0] => Array ( [key] => rat1 [value] => -10 [options] => 0 [type] => 0 ) [1] => Array ( [key] => rat1 [value] => -10 [options] => 0 [type] => 0 ) ) [mesg_id] => 1 [parent_id] => 0 [thread_id] => 1 [cat_id] => 3 [folder_id] => 0 [status] => 4 ) )

Output:

Array ( [0] => Array ( [attrs] => Array ( [0] => Array ( [key] => rat1, [value] => -10, [options] => 0, [type] => 0, ), [1] => Array ( [key] => rat1, [value] => -10, [options] => 0, [type] => 0, ), ), [mesg_id] => 1, [parent_id] => 0, [thread_id] => 1, [cat_id] => 3, [folder_id] => 0, [status] => 4, ), ), $HASH1 = { 0 => { attrs => { 0 => { key => 'rat1', options => 0, type => 0, value => -10 }, 1 => { key => 'rat1', options => 0, type => 0, value => -10 } }, cat_id => 3, folder_id => 0, mesg_id => 1, parent_id => 0, status => 4, thread_id => 1 } };

--shmem

_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2026-04-16 08:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.