#!/usr/bin/perl -w
# unless you're stupid or a genious who has a good reason...
use strict;
# welcome to the CGI mailing list. We often...
use CGI;
# get the information from an HTML form and stuff it into a new "$cgi" thingy
my($cgi) = CGI->new();
# from the form data we just got, get all options that were selected in
# the HTML form select box named "addOns".
my(@addOns) = $cgi->param('addOns');
# if you want to see an example of the output generated by this code when
# a search is successful, uncomment the next line.
# @addOns = qw( far blue car ADDONS );
# make a new array variable. we'll be using it later.
my(@aOns) = ();
# make a new scalar variable. we'll also be using it later.
my($trash) = '';
# make a new array variable filled with the values of all options available
# in the HTML form select box we just mentioned
my(@accountAddOns) = qw( foo bar baz blue car jazz flew far as );
# start doing something. scan over all values in the array named "@addOns"
foreach (@addOns) {
# if the value that we are currently scanning over is equivalent to
# one of the items in "@accountAddOns", save it in the array named
# "@aOns" an mark it as a selected item.
if (isin($_,@accountAddOns)) {
push
(
@aOns,
qq[<option selected="selected">$_</option>]
);
next;
}
# otherwise, save it in the array named "@aOns" as an ordinary item
# as long as it isn't a junk value we don't want.
push
(
@aOns,
qq[<option>$_</option>]
)
and next unless ($_ eq 'ADDONS');
# assign the value of the junk item to the scalar variable named "$trash"
$trash = $_
}
# proudly tell the world what selected options from the HTML form select box
# were equivalent to one of the values in the array named "@accountAddOns"
print(<<__MATCHES__) and exit if @aOns;
Victory! The person who filled out our form selected something that matched
a value in "\@aOns" All matching selections made by that person are listed
below:
@aOns
__MATCHES__
print(<<'__NO_DICE__') and exit;
Our search was fruitless. None of the options selected by the person who
filled out our form were matches for any of the values listed in the
array named "@aOns"
Maybe something went wrong?
__NO_DICE__
# this is a scanner routine that checks to see if its first argument is
# found in the list of all its other arguments. I'm too lazy to explain
# how it works.
sub isin {
my($cmp) = shift(@_) || return(undef);
my(@list) = @_;
for (my($i) = 0; $i < @list; ++$i) {
$list[$i] = '' unless defined($list[$i]);
if ($list[$i] eq $cmp) { return($list[$i],$i) }
}
''
}