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

Well, the below sub works great but it seems a bit repetitious, to me at least. So can someone take a look at and fix up my 2:30am sub? :) In case your wondering, this sub chops up the query string into a hash. It also puts data from select boxes with multiple values into an array inside the hash.
sub formatParse { my $buffer = shift; my (%temp, %count, @params); foreach my $pair (split /[&;]/, $buffer) { my ($name, $value) = split /=/, $pair; $name =~ tr/+/ /; $name =~ s/%([a-fA-F0-9]{2})/pack("c", hex($1))/eg; $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9]{2})/pack("c", hex($1))/eg; push(@params, [$name, $value]); $count{$name}++; } foreach my $key (@params) { $coun{$key->[0]} = 0 unless $coun{$key->[0]}; if ($count{$key->[0]} == 1) { $temp{$key->[0]} = $key->[1]; } else { $temp{$key->[0]}->[$coun{$key->[0]}] = $key->[1]; $coun{$key->[0]}++; } } return %temp; }
Thanks!

Replies are listed 'Best First'.
Re: Better way to do this?
by Beatnik (Parson) on Jun 03, 2002 at 06:45 UTC
    What's wrong with using CGI for form parsing??
    use CGI qw(param); my @keys = param();

    Greetz
    Beatnik
    ... Quidquid perl dictum sit, altum viditur.
Re: Better way to do this?
by Abigail-II (Bishop) on Jun 03, 2002 at 10:09 UTC
    Perhaps you should use use strict; in the future as well (and use CGI;). You seem to use both %count and %coun. I think the latter is a typo, one that could have been found at compile time if you would have used some strictness.

    As for not using CGI.pm, you are free to not use it, but perhaps you shouldn't ask here then if you can't make your code work.

    Abigail

      but perhaps you shouldn't ask here then if you can't make your code work.

      He's free to ask for assistance if he wants to try and create his own CGI module. I would not hesitate to help him and I'm sure there are plenty of other people who will provide him with good advice as well.

Re: Better way to do this?
by Anonymous Monk on Jun 03, 2002 at 06:48 UTC
    A reply falls below the community's threshold of quality. You may see it by logging in.