bassplayer has asked for the wisdom of the Perl Monks concerning the following question:

Greetings Esteemed Monks,

Please forgive me if this question has been asked before. I Googled and Super Searched no avail. When using CGI.pm, I am wondering if there is any way to get the params in the order that they were in the form from which they came.

I recently had to dump a large (50+ fields) form into a text file and an email as quickly as possible. I dumped them into a hash (as I often do):

my @params; foreach ( $q->param() ) { push @params, $_; push @params, $q->param($_); } my %param = @params;
and then put them into the two destinations:
foreach my $key ( keys %param ) { print FILE "$key\t$param{$key}\n"; $email_content .= "$key:\t$param{$key}\n"; }
This was a quick fix to meet an irrational deadline. It was code to be replaced later when we had more time to properly insert the data into the appropriate db tables. I was then informed that for the email, the information needed to be in the order it was in on the form, and so I spent quite a bit of time arranging them by typing each one out.

$email_content .= "email_address:\t$param{email_address}\n";
Could this have been done another way? I could see this coming up again in a few months.

bassplayer

Replies are listed 'Best First'.
Re: CGI Parameters in order
by Mr. Muskrat (Canon) on May 03, 2002 at 17:40 UTC
Re: CGI Parameters in order
by tadman (Prior) on May 03, 2002 at 17:52 UTC
    You can quickly get %param using a single line:
    my %param = $q->Vars();
    These params can be sorted based on a pre-defined order, something not unlike:
    my @order = qw[ firstname lastname age shoesize ]; my %ordered_param = map { $_ => 1 } @order; foreach (@order, sort grep { !$ordered_param{$_} } keys %param) { print "$_ $param{$_}\n"; }
    This will show your ordered parameters, followed by any unordered parameters in alphabetical order.
Re: CGI Parameters in order
by erikharrison (Deacon) on May 03, 2002 at 17:53 UTC

    Matt++

    Since you can't guarentee that the values were submitted to CGI.pm in a usable order, then I'd do this if it comes up again.

    @value_names = qw(all the values that my script uses in order); foreach $name (@value_names) { print "$name:\t$param{$name}"; }

    Note: This little code snippet does not properly handle multi valued fields. It's left as an exercise for someone whose hands aren't sick of typing. ;-)

    Cheers,
    Erik
Re: CGI Parameters in order
by tachyon (Chancellor) on May 04, 2002 at 07:36 UTC

    The order that the params are supplied in is browser dependent but CGI.pm strives to retain this (mostly it is top to bottom on the form):

    my @order_sent = $q->param; foreach my $param (@order_sent) { print "param ", $param, " value ", $q->param($param), "\n"; }

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: CGI Parameters in order
by ehdonhon (Curate) on May 03, 2002 at 19:08 UTC

    This is wrong:

    push @params, $q->param($_);

    The problem is that input fields can have more than one value. When this happens, CGI::param() will return an array of values. This will break things when you later decide to convert your array into a hash.

    Another problem is that you are relying on the client browser to send the data to you in the same order that it appears on the form. I think the only way you can be guaranteed to get the data in the correct order is if you store a configuration that you can use later to put the data in the correct order.

      The problem is that input fields can have more than one value. When this happens, CGI::param() will return an array of values. This will break things when you later decide to convert your array into a hash.

      Yes, I simplified it for this example. Normally, I exclude from the hash any upload fields (handling them separately), and any fields that have multiple values, instead putting them into arrays.

      Another problem is that you are relying on the client browser to send the data to you in the same order that it appears on the form. I think the only way you can be guaranteed to get the data in the correct order is if you store a configuration that you can use later to put the data in the correct order.

      Previously, when I have put CGI parameters into a hash, I was not relying on their order. In the scenario described above, I was hoping to, to avoid 'manually' putting the data in order. The whole point of my question was to find out if I could do this. I have found my answer from the helpful responses above, which is no. As usual with this site, I also learned several better ways to do things. Thanks everyone.

      bassplayer