webcs
asked on
Sorting form pairs in perl
I am parsing form pairs from a web form, but can not figure out how to then sort them alphabetically. Here is the code I am using below.
I have tried @form = sort @form;
but that doesn't seem to work.
------
sub form_parse {
read (STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs)
{
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9] )/pack("C" , hex($1))/eg;
$form{$name} = $value;
}}
I have tried @form = sort @form;
but that doesn't seem to work.
------
sub form_parse {
read (STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs)
{
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9]
$form{$name} = $value;
}}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
It doesn't change %form, but it changes @keys
What are you doing with @keys and %form that did not change?
What are you doing with @keys and %form that did not change?
see
perldoc -q "sort a hash"
ans perhaps
perldoc Tie::IxHash
perldoc -q "sort a hash"
ans perhaps
perldoc Tie::IxHash
ASKER
Ok your replies led me in the right direction, as I just looked up HASH sort and got lots of info on google.
Ended up using this:
foreach $key (sort keys %form) {
# Print the name and value pairs in FORM array to MAIL.
print MAIL "$key: $form{$key}";
}
Thanks for the help. For everyone reference that sorts alphabetically upwards on the key. thanks
Ended up using this:
foreach $key (sort keys %form) {
# Print the name and value pairs in FORM array to MAIL.
print MAIL "$key: $form{$key}";
}
Thanks for the help. For everyone reference that sorts alphabetically upwards on the key. thanks
ASKER
@keys = sort keys %form;
at the end and it didn't change the order at all.