OK, since noone replied to my late-night question of yesterday, I gave it a 
go and came up with the following code, which seems to work but, as all my 
code, seems clumsy, and I don't really understand what I did here:

A form returns a query string like 
"userid=someuser&copies=1&dbkey=25&copies=5&dbkey=27&copies=0&dbkey=29&db=d
efault".
This is an order form which contains a list of books. Next to each item 
there is an input field where the user enters the amount of copies they 
want to order. "dbkey" is the database key for the ordered item, passed to 
the query-string through a hidden input field.

I want to extract all values of "copies" and "dbkey" and associate them 
with each other such that each value of "copies" is associated with each 
following value of "dbkey".

Here's the code I wrote, using CGI.pm;

use CGI qw(:standard);
$query = new CGI;
@names = $query->param;

foreach $name (@names) {
        if ($name eq "copies") { push (@copies, param('copies'));       }
        elsif ($name eq "dbkey") { push (@dbkey, param('dbkey'));       }
        }

# now we have all values of "copies" stored in the array @copies, and all 
values of "dbkey" stored in @dbkey.

my %pairs;

for ($i = 0; $i < @dbkey; $i++) {
        $pairs{"$dbkey[$i]"} = "$copies[$i]";
        }

# this creates the hash %pairs which associates each element of the array 
@copies with the matching element of the
# array @dbkeys, presuming that the "matching" elements have the same index 
number.
# I'm using "dbkey" as the key, because I can guarantee that values of 
"dbkey" will be unique.

I thought problems might occur when no input is given for "copies", i.e. 
when the query-string reads something like "&copies=&dbkey=25".
But actually, such pairs are not included in the hash. This is just how I 
want it to be, though somehow I'd like to understand why that is the case.

Just in case a user mistakenly types "0" for a book they don't want to 
order, I thought I'd add the following code to remove all key/value pairs 
from the hash where the value is zero:

foreach $key (keys %pairs) {
        if ($pairs{$key} == 0) {
            delete @pairs{"$key", "$pairs{$key}"};
        }
    }


Birgit Kellner


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to