Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Ok, this script I am working on allows people to choose 5 different names in drop down menus (one name per drop down menu).. so I have 5 drop down menus with the same list of names and I am saving them into an array like this:
@names = ($INPUT{'name1'},$INPUT{'name2'},$INPUT{'name3'}, $INPUT{'name4'},$INPUT{'name5'});
Ok, what I don't want people to be able to choose the same name twice or three times, or so on. So is there a quick way to compare all the names, see if any are equal, and return an error? I am still learning Perl, so the only way I know how to do this is like so
if ($INPUT{'name1'} eq $INPUT{'name2'}) { $error = 'duplicate vote'; &error; }
But this method would take up a lot of space by making each comparison one at a time... so is there an easier way??? Thanks in advance if you can help me :)

Replies are listed 'Best First'.
You're using CGI.pm, Right?
by chromatic (Archbishop) on Jan 23, 2000 at 05:02 UTC
    In HTML forms, there's a widget that supports multiple selections. You can pick more than one if you use CTRL+click or whatever your OS/GUI/browser widget implementation allows.

    The secret is the scrolling_list() subroutine in CGI.pm.

    In any modern installation of Perl, just type 'perldoc CGI' at the command line, and you'll be in business.

    If that's not really want you're looking for (simplify), I would just assign the parameters to a hash, which will get rid of duplicates. Then just iterate over the keys and grab the values. That's a FAQ somewhere.

Re: Question about comparing data values..
by vroom (His Eminence) on Jan 20, 2000 at 23:07 UTC
    you could do something like:
    for($i=1; $i<=5; $i++){ if($checked{$INPUT{"name$i"}}){ &error='duplicate vote'; last; } else { $checked($INPUT{"name$i"}}=1; } }
Re: Question about comparing data values..
by dlc (Acolyte) on Jan 25, 2000 at 00:32 UTC

    how about something like

    my %names = ();
    map { $names{$_}++ } grep /^names/ keys %INPUT;

    this creates a hash %names where each key is unique (earlier entries get clobbered by later ones)--which is what you want--with throwaway values. access the entries by using keys %names.

Re: Question about comparing data values..
by Anonymous Monk on Jan 24, 2000 at 16:54 UTC
    I would create a hash which keys would be the values the user has selected. If there are double entries, some of them will be overwritten. Just count how many keys your hash has, and if it's less than 5, that means entries are doubled (or tripled, or ...)
    $entries{$INPUT{$_}} foreach (keys %INPUT); if (%entries < 5) { $error = "duplicate vote"; &error; }