Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^2: Convert stirng to array of hashes

by codeacrobat (Chaplain)
on Aug 08, 2009 at 20:34 UTC ( [id://787097]=note: print w/replies, xml ) Need Help??


in reply to Re: Convert stirng to array of hashes
in thread Convert stirng to array of hashes

You can also build a parser with TheDamian's Regexp::Grammars. Here is a proof of concept, which you can improve.
use Regexp::Grammars; my $parser = qr{ (?: <[Hash]> ** ( , ) ) <token: Hash> \{ <[KV]> ** ( , ) \} <token: KV> (?: \s* <[Key]> \s* => \s* <[Value]> \s* ) <token: Key> \w* <token: Value> ' \w* ' }x; use Data::Dumper; # Match the grammar against some text... if ("{ foo => 'bar', baz => 'bof' }, {bing => 'go'}" =~ $parser) { # If successful, the hash %/ will have the hierarchy of results.. +. print Dumper(\%/ ); } __END__ $VAR1 = { '' => '{ foo => \'bar\', baz => \'bof\' }, {bing => \'go\'}' +, 'Hash' => [ { '' => '{ foo => \'bar\', baz => \'bof\' }', 'KV' => [ { '' => ' foo => \'bar\'', 'Value' => [ '\'bar\'' ], 'Key' => [ 'foo' ] }, { '' => 'baz => \'bof\' ', 'Value' => [ '\'bof\'' ], 'Key' => [ 'baz' ] } ] }, { '' => '{bing => \'go\'}', 'KV' => [ { '' => 'bing => \'go\'', 'Value' => [ '\'go\'' ], 'Key' => [ 'bing' ] } ] } ] };

print+qq(\L@{[ref\&@]}@{['@'x7^'!#2/"!4']});

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2024-04-23 19:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found