Gossamer Forum
Home : General : Perl Programming :

Help with hash and array!

Quote Reply
Help with hash and array!
Hi Paul,

Do you have a better way of doing this? I am quite new to perl programing. Even thought it works but I believe there should be a better way of doing it. I don't think another foreach loop in a foreach loop is the right way.

I have a list of search engines in a multiple select box form. When multiple option is selected, these data will be stored separated by commas. I would like to retrieve the data and have those selected options to appear as selected in the multiple select box.


Please advise. Thanks in advance.Crazy


# Search engine hash list
my %searchengine = ('1','Yahoo','2','Google','7','AltaVista','10','Ex
cite');

# Value parsed from a multiple select box
my @searchenginemulti=split(/,/,$Form{'searchengine'});

# To arange the searchengine hash list alphabetically
%searchenginereverse = reverse %searchengine;

my $ops='<select name="searchengine" size="6" multiple>';
foreach (sort keys %searchenginereverse){
$ops .= "<option value=\"$searchenginereverse{$_}\"";
foreach $searchenginemulti(@searchenginemulti) {
if ($searchenginereverse{$_} eq $searchenginemulti){
$ops .= " selected"
}
}
$ops .= ">$_\n";}
$ops .= '</select>';

Last edited by:

james678: Dec 18, 2002, 6:35 PM
Quote Reply
Re: [james678] Help with hash and array! In reply to
Hehe there are other people besides me who can answer but I'm flattered you asked me personally Smile

Could you please explain a little further. I was a bit confused. What is the %searchengine hash for?

Quote:
When multiple option is selected, these data will be stored separated by commas. I would like to retrieve the data and have those selected options to appear as selected in the multiple select box.

I'm not sure I understand this either. If the engines are already selected, why are you getting the values and then creating a select list with them selected again?
Quote Reply
Re: [james678] Help with hash and array! In reply to
I think I see what you're asking.
Try this - no need for the serachenginreverse hash, or nested loops.

Code:
# Search engine hash list - same as before.
my %searchengine = (1 => 'Yahoo', 2 => 'Google', 7 => 'AltaVista', 10 => 'Excite');
# Use the param() call from CGI.pm - always use CGI.pm to parse CGI input - it works properly!
my @searchenginemulti = param('searchengine');

my $ops = qq(<select name="searchengine" size="6" multiple>\n);

# Set $chosen{$engine} to 1 for each $engine in @searchenginemulti
my %chosen;
@chosen{@searchenginemulti} = (1) x @searchenginemulti;

# Iterate through engines in alphabetic order
foreach my $engine (sort { $searchengine{$a} cmp $searchengine{$b} } keys %searchengine) {
# Use qq() alternate quoting to remove the need for ugly backslashes before quotes.
$ops .= qq(<option value="$engine");
$ops .= qq( selected) if $chosen{$engine};
$ops .= qq(>$searchengine{$engine}</option>\n);
}

$ops .= qq(</select>\n);

Cheers,
Dave.