in reply to Construct a data structure from a string
#! perl -slw use strict; use Data::Dumper; my $string = "foo[1].bar.baz[0] = 123"; my @bits = split '\.| = ', $string; my $data = pop @bits; m/(\w+)(?:\[(\d+)\])?/ and $data = { $1 => $2 ? [ (undef) x $2, $data ] : $data } for reverse @bits; print Dumper $data; __END__ P:\test>393104 $VAR1 = { 'foo' => [ undef, { 'bar' => { 'baz' => '123' } } ] };
|
|---|