converting perl hash to javascript hash

converting perl hash to javascript hash
use warnings;
use strict;
use CGI qw(:standard);
my %animal = ( fish => 'coelacanth', canid => 'Channel Island fox', bird => 'moa' ); my @pairs; for my $type ( keys %animal ) { push @pairs, "'$type':'$animal{$type}'"; } my $js_string = 'new Object({' . join("\n,", @pairs) . '});';
print header(), start_html(), <<"JSend", <script type="text/javascript"> var obj = $js_string; for ( var attr in obj ) { document.write(attr + ' --> ' + obj[attr] + '<br />'); } </script> JSend end_html();
Remember JavaScript has an eval function so you can add stuff on the fly if you’re doing AJAX or something.
[close] Permanent link · http://querylog.com/q/converting+perl+hash+to+javascript+hash

Suggested HTML for linking:
Link preview: converting perl hash to javascript hash
18 July 2005 · Internet & computing
The page found by the original query:
Code snippets in Perl and JavaScript
Browse by the page—15 Q&R each
« previous | more »