awohld has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl -w use CGI::Carp qw(fatalsToBrowser); use strict; use CGI; use Data::Dumper; my $cgi = CGI->new(); my %cgiParam = $cgi->Vars; # Store CGI params into a hash my @cgiParamKeys = keys %cgiParam; # Store the %cgiParam keys into an +array my @cityKeys; my @distanceKeys; foreach( @cgiParamKeys ) { if( /city/ ) { #If a hidden city field found store it in an array; push @cityKeys, $_; #Store the field in an array @cityKeys = sort @cityKeys; #Sort the array } if( /distance/ ) { #If a hidden distance field found, store it in an + array push @distanceKeys, $_; #Store it in the array @distanceKeys = sort @distanceKeys; #Sort the array } } my $numLocations = @cityKeys; #Store the last city and distance submited as the last City and distna +ce element in the hash $cgiParam{"city$numLocations"} = $cgiParam{'postCity'}; $cgiParam{"distance$numLocations"} = $cgiParam{'postDistance'}; ### #Store new keys into array used for printing hidden keys push @cityKeys, "city$numLocations"; push @distanceKeys, "distance$numLocations"; ### my @hiddenKeys = qq(@cityKeys @distanceKeys); print $cgi->header; print Dumper %cgiParam; print "<br><br>"; print Dumper @hiddenKeys; print $cgi->start_form(-action=>'help.pl'); foreach( @hiddenKeys ) { if(/city/) { print $cgi->hidden("$_", "$cgiParam{$_}"); } if(/distance/) { print $cgi->hidden("$_", "$cgiParam{$_}"); } } print <<EOF; <select name="postCity" tabindex="1"> <option value="1">Columbus</option> <option value="2">Chicago</option> </select>    Distance: <input type="text" name="postDistance" si +ze="6" maxlength="6" /> <br> EOF print $cgi->submit('Go'); print $cgi->end_form;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Trouble printing default variable in "foreach" loop.
by pg (Canon) on Sep 14, 2005 at 03:45 UTC | |
|
Re: Trouble printing default variable in "foreach" loop.
by Tanktalus (Canon) on Sep 14, 2005 at 03:20 UTC | |
|
Re: Trouble printing default variable in "foreach" loop.
by GrandFather (Saint) on Sep 14, 2005 at 03:24 UTC |