Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

hashes in CGI.pm

by rbi (Monk)
on Feb 26, 2001 at 20:38 UTC ( [id://60881]=perlquestion: print w/replies, xml ) Need Help??

rbi has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
I'm trying to learn the use of hashes with CGI.pm. Here is a short code where I create an array of hashes (think so.. :) ), and then I use it in a form created with CGI.pm.
#!/usr/bin/perl -w use CGI qw/ :html3 /; my %hash; my $key; my %names; my $value; my $i; foreach $i (1..10) { $key = $i % 3; $value = $i; $hash{$key} ||= []; push @{$hash{$key}}, $value; } $query = new CGI; print $query->header(); foreach $key (0..2) { $names[$key] = "key $key"; print $query->p(join( '/', @{$hash{$key}}), " modulo 3 give $key +\n"); } print $query->start_form(); # print $query->table( TR([ th(@names), td(print $query->popup_menu ( -name=>'0', -values=>[@{$hash{0}}], -default=>$hash{0}) ), td(print $query->popup_menu ( -name=>'1', -values=>[@{$hash{1}}], -default=>$hash{1}) ), td(print $query->popup_menu ( -name=>'2', -values=>[@{$hash{2}}], -default=>$hash{2}) ), # # Instead, I'd like to write a single instruction # I know this can't work ... it's just a note # # td($query->popup_menu ( -name=>"$key", # -values=>[@{$hash{$key}}], # -default=>$hash{$key}) # ), ]) ); print $query->end_form(); print $query->end_html();
How can I generate those popup menus in a single instruction (or in any case without explicitly pointing at the keys of the hashes)?
thanks in advance.
Roberto

Replies are listed 'Best First'.
Re: hashes in CGI.pm
by davorg (Chancellor) on Feb 26, 2001 at 21:04 UTC

    I've tidied the code up a bit and made the change that you requested. The simplest way to do what you wanted with the table rows is to use map as I've demonstrated below.

    #!/usr/bin/perl -w use strict; use CGI qw/ :standard /; my %hash; my $key; my %names; my $value; my $i; foreach $i (1..10) { $key = $i % 3; $value = $i; # The next line is not required due to autovivification # $hash{$key} ||= []; push @{$hash{$key}}, $value; } my @names; foreach $key (0..2) { $names[$key] = "key $key"; print p(join( '/', @{$hash{$key}}), " modulo 3 give $key\n"); } print start_form(); print table(Tr([th(@names), map { td(popup_menu(-name=>'0', -values=>[@{$hash{$_}}], -default=>$hash{$_})) } keys %hash +])); print end_form(); print end_html();
    --
    <http://www.dave.org.uk>

    "Perl makes the fun jobs fun
    and the boring jobs bearable" - me

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://60881]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-25 13:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found