Solved

Sorting form pairs in perl

Posted on 2007-11-26
6
275 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

726 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