in reply to Construct a data structure from a string
It's single pass, but uses a tmp variable, which is somewhat clunky. Oh well.sub str2data { my($string, $value) = @_; my $tmp; for my $part (reverse split /\./, $string) { undef $tmp; if ($part =~ /([^][]*?)\[(\d*?)\]/) { $tmp->{$1}[$2] = $value; $value = $tmp; } else { $tmp->{$part} = $value; $value = $tmp; } } return $value; }
|
|---|