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

Esteemed monks, I am writing a CGI that has to emulate another CGI which is no longer avialble to handle our data.

Part of the form returns a series of option values. These are named: op1 through op44. None need be present, but those that do appear need to be handled in numerical order. Each option has a value that looks like this:

Description part - numeric part
The description part can have HTML markup included.

I am using the CGI module.

Now, I can think of a brute force and rather ignorant way to handle getting this information into a hash of hashes. But I am looking for an elegant solution!

Does anyone have any suggestions?

John

Replies are listed 'Best First'.
Re: CGI - Parsing many options.
by Cody Pendant (Prior) on Aug 01, 2003 at 04:34 UTC
    I don't actually quite understand what your question is.

    What do you want to do with this information?

    If it's been passed by CGI.pm, it's already in a hash for you, or it can be.

    Are you saying that $op1 contains two scalars separated by a hyphen and you don't know how to split it?

    Please give more detail -- some examples of the values and what you want to do with them.

    Oh, and if you've got more than forty fields in a form, it might be time to think multi-page...



    ($_='kkvvttuubbooppuuiiffssqqffssmmiibbddllffss') =~y~b-v~a-z~s; print
      Cody:

      Seems that I should not ask questions after midnight!

      Some points:

    • I can split the two scalars, thats isn't a problem :)
    • The options need not appear on the form, they can be hidden fields.
    • I would ideally like to be able to refer to them like this: $value = $options{option#}{scalar1 or scalar2}

      Here is what is happening, we have been using a hosted shopping cart for some years, we can no longer use it because they do not support our new payment processor. Because we have a huge base of statically coded HTML pages, going back to 1996, built on this cart I need to emulate its operation. In a simple example this is what I get from the form:

      item : p-1723^I-0100^Internet book^op1op3op5^1^^^^^^ op1 : Single copy - $69.95 op3 : Hard cover - $10.00 op5 : Signed - $10.00
      Now, the right hand portion of the option, if present, in the case of options1, 3 & 5 will be added to produce the final price. In this example the total price is op1+op2+op3 or $89.95.

      The left portion, the text part, will be appended to the description. However, here comes my twist, according the documentation for the original script, op2 and op4 if returned from the form needs to be interpolated inthe description, it could be like this:

      op2 : <br>Cover: op4 : <br>
      So that when the description is displayed it will look like:
      Single copy Cover: Hard cover Signed
      In a single cell in a table.

      My question is this. How would you, or the other monks, suggest I create the hash I would like to use. Or even better, should I be using a hash like that at all?

      Your respectful Perl novice,

      John

        Do also remember that the CGI script should validate what it gets from the form. Remember that the user can save the form on his/her machine, alter the numbers in the saved page, and then submit the altered form. "Gee, I don't think signed is worth $10.00. I'll make it $0.00."

        Never trust the browser.

        You're probably already doing this, but a reminder never hurts.

Re: CGI - Parsing many options.
by tcf22 (Priest) on Aug 01, 2003 at 13:51 UTC
    There is no CGI method that will do what you need to my knowledge. The Vars() will return a hash in the format:
    %hash = ( op1 => 'Desc1 - Num1', op3 => 'Desc3 - Num1', ... );
    Try something like this to build your hash of hashes.
    use strict; use CGI; my $cgi = new CGI; my %data = $cgi->Vars(); my %result; foreach(sort {substr($a,2) <=> substr($b,2)} keys %data){ my ($desc, $num) = split(/ ?- ?/, $data{$_}); $result{$_} = {Desc => $desc, Num => $num}; }