[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Sorting form pairs in perl

Posted on 2007-11-26
6
Medium Priority
?
281 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 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
Industry Leaders: 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying 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

In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

650 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