For Programmers: Free Programming Magazines  


Home > Archive > PERL CGI Beginners > September 2004 > ordered hash









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 ordered hash
Brian Larochelle

2004-09-24, 3:55 pm

Hello,

I was hoping to get a little advice creating a cgi form. I want to
create a popup menu to list all the states in the US( and then later
countries), I want to store all the values in variables to pass to the
subroutines later on. The below code works, I just have the values for
the states stored in a hash which is unorderd and diplayed as such on
the form. I would like the popup menu display to keep the order from
below. Also adding modules like Hash::Ordered and Tie::IxHash isn't an
option. Is there a better way of doing this or am I just making it
overly complicated? Thanks for any help you can give.

#!/usr/bin/perl
use warnings;
use strict;
use CGI qw(:standard escapeHTML);

use CGI::Carp qw(fatalsToBrowser);
BEGIN {open(STDERR,">error.log");}

my (@address, @state, $choice);

@address =
({name => "last_name", label => "Last name:", size => 20,
req => 1},
{name => "first_name", label => "First name:", size => 20,
req => 1},
{name => "business_name", label => "Business name:", size => 40},
{name => "address1", label => "Street address:", size => 40,
req => 1},
{name => "addreess2", label => "", size => 40},
{name => "city", label => "City:", size => 20,
req => 1}
);
@state =
({name => "state/prov", label => "State/Prov:", values => {"AK" =>
"Alaska", "AL" => "Alabama",
"AR" => "Arkansas", "AZ" => "Arizona" => "CA" =>
"California", "CO" => "Colorado",
"MT" => "Montana", "NC" => "North Carolina", "ND" =>
"North Dakota", "NE" => "Nebraska",
"NH" => "New Hampshire", "NJ" => "New Jersey", "NM" =>
"New Mexico", "NV" => "Nevada",
"NY" => "New York", "OH" => "Ohio", "OK" => "Oklahoma",
"OR" => "Oregon", "PA" => "Pennsylvania",
"PR" => "Puerto Rico", "RI" => "Rhode Island", "SC" =>
"South Carolina", "SD" => "South Dakota",
"TN" => "Tennessee", "TX" => "Texas", "UT" => "Utah", "VA"
=> "Virginia", "VI" => "Virgin Islands",
"WA" => "Washington", "WI" => "Wisconsin", "WV" => "West
Virginia", "WY" => "Wyoming",
"OTHER" => "OTHER (Outside USA)"}}
);
#************************
foreach $f (@{$address}) {
$label = $f->{label};
$label .= " *" if $f->{req}; # add asterick for required fields
push(@row, Tr(td(escapeHTML($label)),
td(textfield(-name => $f->{name}, -size =>
$f->{size}))
));
}
print table(@row);

foreach $f (@{$state}) {
$label = $f->{label};
push (@row, Tr ( td (escapeHTML ($label)),
td (popup_menu (-name => $f->{name}, -value =>
$f->{values}))
));
}
print table(@row),
submit(-name => "choice", -value => "Submit"),













Chris Devers

2004-09-24, 3:55 pm

On Fri, 24 Sep 2004, brian larochelle wrote:

> ordered hash


Hashes are, almost by definition, unordered lists of key/value pairs.

If you want to work with one in order, add a sort command to accesses:

my @sorted_keys = sort keys %hash;

foreach $key @sorted_keys {
my $value = $hash{$key};
}

etc.

Or just

foreach $key ( sort keys %hash ) {
my $value = $hash{$key};
}

to skip the temp variable.

Make sense ?



--
Chris Devers
Brian Larochelle

2004-09-24, 3:55 pm

Thank you for your quick response Chris.

That makes perfect sense. Except for the last value of 'other' which I
would want at the end of the list and a choice for the person filling
out the form is they did not live in the US.

Chris Devers wrote:

>On Fri, 24 Sep 2004, brian larochelle wrote:
>
>
>
>
>Hashes are, almost by definition, unordered lists of key/value pairs.
>
>If you want to work with one in order, add a sort command to accesses:
>
> my @sorted_keys = sort keys %hash;
>
> foreach $key @sorted_keys {
> my $value = $hash{$key};
> }
>
>etc.
>
>Or just
>
> foreach $key ( sort keys %hash ) {
> my $value = $hash{$key};
> }
>
>to skip the temp variable.
>
>Make sense ?
>
>
>
>
>

Chris Devers

2004-09-24, 3:55 pm

On Fri, 24 Sep 2004, brian larochelle wrote:

> That makes perfect sense. Except for the last value of 'other'
> which I would want at the end of the list and a choice for the
> person filling out the form is they did not live in the US.


A common way around this is to leave "other" out of the hash:

do_stuff( "other" );
foreach $key ( sort keys %hash ) {
do_stuff( $hash{$key} );
}
do_stuff( "final thing" );

sub do_stuff {
my $thingy = shift;
...
print qq[ <p> $thingy </p> ];
}

This general approach can be applied to lots of situations, including
the one that you're describing.



--
Chris Devers
Sponsored Links







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

Copyright 2008 codecomments.com