For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > November 2007 > Re: making the keys of a hash from parameters collected from a









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: making the keys of a hash from parameters collected from a
mike

2007-11-29, 4:25 am

On Nov 28, 11:11 pm, mike <rall...@adelphia.net> wrote:
> Hello everyone:
> I have the need to prioritize a number of items from a list and then
> print them out in the order in which they were chosen. To be more
> specific, I have a list of 12 items and I would like to have the users
> indicate which is their first choice, which is their second, etc. up
> to 8. I must put the items into a file in the order which has been
> prioritized by the user. My thought was to present a form with 12
> textboxes, into which they can type the number '1' for their first
> choice, the number '2' for their second, etc. Then, when parsing the
> form, I could make a hash whose keys are the numbers put into the
> boxes, from 1 to 8, with the hash 'values' being the textbox 'names'.
> Sorting the hash according to keys would then give me the info I
> need. I find, however, that this approach is flawed in that the
> collected parameters from the forms are not in the proper format,
> apparently, for making into keys. A script or 2 is worth a zillion
> words, so I present scaled-down versions of the form and the parsing
> script, and if anyone has a solution I sure would like to hear it.
> The first script, 'mike.cgi' follows:
>
> #!/usr/bin/perl -wT
> use CGI::Carp qw(fatalsToBrowser);
> use CGI qw(:standard -no_xhtml);
> use strict;
> use diagnostics;
> my $q = new CGI;
> print $q->header;
> print $q->start_html(-title=>"mike.cgi",-bgcolor=>"silver",-
> text=>"blue",-link=>"blue"),$q->br,$q->br,$q->br;
> print $q->h3("Please indicate your favorite fruit with a '1', and your
> second favorite with a '2'. \n");
> print $q->h3("Leave the rest blank:\n");
> print $q->start_form(-method=>"POST", -action=>"parse.cgi");
> print $q->start_form(-method=>"POST", -action=>"getparam.cgi");
> print $q->h3(textfield(-name=>'app', -size=>1),"apple");
> print $q->h3(textfield(-name=>'ora', -size=>1),"orange");
> print $q->h3(textfield(-name=>'lem', -size=>1),"lemon");
> print $q->h3(textfield(-name=>'lim', -size=>1),"lime");
> print $q->h3(textfield(-name=>'ban', -size=>1),"banana");
> print $q->submit, $q->reset, $q->endform, end_html;
> exit;
>
> The script that is supposed to make and sort the hash is called
> 'parse.cgi':
> #!/usr/bin/perl -wT
> use CGI::Carp qw(fatalsToBrowser);
> use CGI qw(:standard -no_xhtml);
> use strict;
> use diagnostics;
> my $q = new CGI;
> print $q->header;
> print $q->start_html(-title=>"mike.cgi",-bgcolor=>"silver",-
> text=>"blue",-link=>"blue"),$q->br,$q->br,$q->br;
> my %hashfruit=(
> "param('app')"=>'app',
> "param('lem')"=>'lem',
> "param('ora')"=>'ora',
> "param('lem')"=>'lem',
> "param('lim')"=>'lim',
> );
> my $k;
> foreach $k (keys %hashfruit){
> print "<P> The key is: <b> $k </b> and the value is <b>
> $hashfruit{$k}</b>";
> }
> end_html; exit;
>
> As you can see, the 'keys' were intended to be the values of
> parameters passed to 'parse.cgi', but instead of getting the values (1
> and 2), I get as keys the strings 'param(app)' etc. Does anyone
> know how to get the actual contents the user typed in so I can use
> them as the keys to %hashfruit? Thanks in advance.
> Mike

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com