Solved

Sorting form pairs in perl

Posted on 2007-11-26
6
272 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
Comment Utility
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
Comment Utility
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
Comment Utility
Adam I inserted

@keys = sort keys %form;

at the end and it didn't change the order at all.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 84

Expert Comment

by:ozo
Comment Utility
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
Comment Utility
see
perldoc -q "sort a hash"
ans perhaps
perldoc Tie::IxHash
0
 
LVL 2

Author Comment

by:webcs
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In this tutorial I will show you how to provide a dynamic RTF document on your website generated with data from your database. For this tutorial you will need Microsoft Word or WordPad, WhizBase and Microsoft Access. In this tutorial I will show …
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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.

744 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

17 Experts available now in Live!

Get 1:1 Help Now