I have some code that stems from another node. I revised my code to store my data in a hash.

When it comes down to looping through my array of hash keys in a "foreach" loop for the @hiddenKeys array, my $_ variable looks like it's printing the whole array and not just the list element being evaluated.

In this code the output should incrementally add hidden fields for the "city" and "distance" form fields every time the CGI script is posted. So if you submit 5 times you should have 10 (2 form elements in the form) hidden fields. After 3 submissions there should be 6 hidden fields with names: city1, city2, city3, distance1, distance2, distance3.

What am I doing wrong?

Here's my code:
#!/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> &nbsp&nbsp&nbspDistance:&nbsp<input type="text" name="postDistance" si +ze="6" maxlength="6" /> <br> EOF print $cgi->submit('Go'); print $cgi->end_form;

In reply to Trouble printing default variable in "foreach" loop. by awohld

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.