?
Solved

Sorting form pairs in perl

Posted on 2007-11-26
6
Medium Priority
?
283 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 225 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 150 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 85

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 85

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses
Course of the Month16 days, 6 hours left to enroll

850 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