Gossamer Forum
Quote Reply
CGI.pm
Im using CGI.pm to parse some forms and some form fields have the same name with different values. $IN->param() returns multiple values all at once...eg...

Ticking 3 checkboxes with values 1 2 3 would return 123

Now, I have a routine that converts all form fields into a hashref for ease of use ..eg...

$form = { Field => 'Value' };

....but how would I get the code to create something like:

$form = { Field => ['Value1','Value2'] };

....if multiple values are returned? ref won't work.

I need something like $IN->get_hash used in GT's CGI.pm - does anyone know if the original CGI.pm has a build in routine to do this?



Last edited by:

RedRum: Dec 24, 2001, 12:34 PM
Quote Reply
Re: [RedRum] CGI.pm In reply to
oh my gosh.. gossamer forum is so weird. anyways..

the only reason why $in->param ('something') would print out 123 is cause you don't have a whatever the heck its called.. seperator? i think to make one it is $, = ", "; or something.. perl knowledge is very shabby.. but i know $" = ", "; works for in quotes..

yes so anyways.. i'm trying to say.. $in->param ('something') turns into an array when you have more than 1 choice.. @{$in->param('something')}
Quote Reply
Re: [jerrysu] CGI.pm In reply to
Oh thanks....thats pretty simple then...I can just do:

Code:
for ($IN->param) {
if (@{$IN->param($_)} > 1) {
@{$form->{$_}} = @{$IN->param($_)}
}
else {
$form->{$_} = $IN->param($_)
}
}




Last edited by:

RedRum: Dec 25, 2001, 2:19 AM
Quote Reply
Re: [RedRum] CGI.pm In reply to
Code:
foreach ($query->param) {
my $value = $query->param($_);
$form{$_} = $value;
}

... would completly not work. I've eaten too much Turkey. Serves me right for logging on today!!!

Jeepers, I need to get a life!

- wil

Last edited by:

Wil: Dec 25, 2001, 8:21 AM
Quote Reply
Re: [Wil] CGI.pm In reply to
Sorry, what do you mean Wil...where did that code come from?

Nevertheless wherever it came from, it would work for single values (although it can be shortened to one line).

foreach ($query->param) { $form{$_} = $query->param($_) }

Last edited by:

RedRum: Dec 25, 2001, 10:16 AM
Quote Reply
Re: [jerrysu] CGI.pm In reply to
Hmm actually Jerry...this won't work:

@{$IN->param('field')};

...however...

local $, = ',';
print $IN->param('field');

....works although I'd need extra code to push the values into an arrayref


Last edited by:

RedRum: Dec 25, 2001, 10:50 AM
Quote Reply
Re: [RedRum] CGI.pm In reply to
ya ok.. i think to make something in perl nowadays i'd have to use a book. i don't remember much.

this gossamer forum thing is pretty interesting. dhtml?
Quote Reply
Re: [jerrysu] CGI.pm In reply to
Here's what I've come up with that works great...

Code:
sub get_hash {
#----------------------------------------------------------------
# Return a hash of all param names and their values.

my (%INPUT,@INPUT);

# Loop through all params and decide what to do.
for ($IN->param) {
next if /-/;
@INPUT = $IN->param($_);

if (@INPUT > 1) {
@{$INPUT{$_}} = @INPUT;
}
else {
$INPUT{$_} = $IN->param($_)
}
}

return %INPUT;

}

Last edited by:

RedRum: Dec 25, 2001, 11:41 AM
Quote Reply
Re: [RedRum] CGI.pm In reply to
Hehe also missed the cool $obj->Dump; method