Solved

Sorting form pairs in perl

Posted on 2007-11-26
6
274 Views
Last Modified: 2013-12-25
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;
      }}
0
Comment
Question by:webcs
6 Comments
 
LVL 39

Accepted Solution

by:
Adam314 earned 75 total points
ID: 20353887
You should use the CGI module to parse the form.

But that aside:
You have a hash %form, this is a completly different variable from @form.
If you want to get the keys sorted:
@keys = sort keys %form.
0
 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 50 total points
ID: 20354630
To expand on Adam's comments.

Only use your own CGI parser if you are 101% familiar with the full CGI specifications and all the various gotchas from none compliant browsers and web servers.

The standard Perl CGI module takes all of this into account.

The code for doing the sorting would be:


#!/usr/bin/perl
use strict;
use CGI;
my $q = new CGI;
my @keywords = sort $q->param;
 
print $q->header('text/plain');
 
foreach my $key (@keywords) {
  print "Keyword: $key\n";
}

Open in new window

0
 
LVL 2

Author Comment

by:webcs
ID: 20358279
Adam I inserted

@keys = sort keys %form;

at the end and it didn't change the order at all.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 84

Expert Comment

by:ozo
ID: 20358329
It doesn't change %form, but it changes @keys
What are you doing with @keys and %form that did not change?
0
 
LVL 84

Expert Comment

by:ozo
ID: 20358360
see
perldoc -q "sort a hash"
ans perhaps
perldoc Tie::IxHash
0
 
LVL 2

Author Comment

by:webcs
ID: 20358890
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
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now