in reply to Re^11: Converting hash into js object properties
in thread Converting hash into js object properties
OK, after some trial and error, I came up with this:
sub hash_walk { my ($hash, $string, $level, %keys_printed) = @_; while (my ($k, $v) = each %$hash) { $string .= "{ $k: " unless $keys_printed{$level}{$k}; $keys_printed{$level}{$k} = 1; if (ref($v) eq 'HASH') { $string = hash_walk($v, $string, ++$level, %keys_printed); $string .= ' }, '; $level--; } else { $string .= $v . ' },'; } } return $string; } my %data = ( email =>{steve => 'owens'}, user => {steve => {fun => 'times', laugh => 'in'}}, pass => {blah => 'moomoo'}, ); print hash_walk(\%data, '', 0, ()) . "\n";
Now I just need to come up with some code to properly handle the quoting of the values.
$PM = "Perl Monk's";
$MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate";
$nysus = $PM . ' ' . $MCF;
Click here if you love Perl Monks
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^13: Converting hash into js object properties
by roboticus (Chancellor) on Nov 22, 2017 at 20:46 UTC | |
|
Re^13: Converting hash into js object properties
by huck (Prior) on Nov 22, 2017 at 05:27 UTC | |
by nysus (Parson) on Nov 22, 2017 at 05:33 UTC |