in reply to converting a String to Hashtable
The grep is needed to filter out spurious zero-length sub-strings produced by the split regex, so you can't have a key or value that is a zero-length string. Note that embedded spaces are preserved within key and value sub-strings, but leading and trailing spaces are stripped.>perl -wMstrict -le "my $s = '{key_1 : value_1}{key_2 : value of 2} {key n : 0}'; my $spl = qr{ \s* [{}:] \s* }xms; my %hash = grep length($_), split $spl, $s; use Data::Dumper; print Dumper \%hash; " $VAR1 = { 'key_1' => 'value_1', 'key_2' => 'value of 2', 'key n' => '0' };
|
|---|