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

I have a form that has 5 sets of fields. They are all numbered accordingly and was wondering if any of you knew a way to process all of them at once.

The fields are: fname1, lname1, phone1, add1, email1, fname2, lname2...

There is 5 of each (fname1 - fname5). I could use if(param("fname1") ... if(param("fname2").. but to me, that's enough repititious code to know there's a better way.

It gets a little tougher. I need to make sure at LEAST the phone# or the email# was filled out. Doesn't matter if both are but at least one must be.

Can someone come up with a one-pass way to go about doing this?

Replies are listed 'Best First'.
Re: retrieving similar form params
by GrandFather (Saint) on Nov 27, 2005 at 23:15 UTC

    What have you go so far? You might like to fake up some code for test purposes that uses __DATA__ or similar for the source of your test data, and prints the test results. Show us the current output from your test code and the output you would like.


    DWIM is Perl's answer to Gödel
      This is what I have.
      if(param("fname1")) { if(param("lname1")) { if(param("phone1") ne "" || param("email1") ne "") { if(param("add1") { # MySQL functions here } } } }
      I have that for each number. And it's a lot longer than it should be, right? That's pretty much what I want to shorten down into one code that processes all of my form fields.

        This may point you in a useful direction:

        use strict; use warnings; my %param = ( fname1 => "Fred", lname1 => 'Bloggs', email1 => 'fred@xyzzy.com', fname2 => "Joe", lname2 => 'Bloggs', email2 => 'joe@plugh.com', fname3 => "Sue", lname3 => 'Brown', phone3 => '555-1234-567', fname4 => "", lname4 => 'Brown', phone4 => '555-1234-567', fname5 => "Liz", lname4 => 'Green', ); my @entries; for my $key (keys %param) { my ($field, $num) = $key =~ /^(.*?)(\d+)/; next if ! defined $num; ${$entries[--$num]}{$field} = $param{$key} || ''; } for my $entry (@entries) { next unless ($$entry{lname} && $$entry{fname}); # Must have a full n +ame next unless ($$entry{phone} || $$entry{email}); # Must have phone or + email $$entry{phone} ||= ''; $$entry{email} ||= ''; print "$$entry{lname},$$entry{fname},$$entry{phone},$$entry{email}\n +"; }

        Prints:

        Bloggs,Fred,,fred@xyzzy.com Bloggs,Joe,,joe@plugh.com Brown,Sue,555-1234-567,

        Update to get closer to OP's requested behaviour


        DWIM is Perl's answer to Gödel

        Now show us a code snippet with a populated version of param and a print that generates the sort of data you want to push into the DB. Something like:

        use strict; use warnings; my %param = ( fname1 => "Fred", lname1 => 'Bloggs', email1 => 'fred@xyzzy.com', fname2 => "Joe", lname2 => 'Bloggs', email2 => 'joe@plugh.com', fname3 => "Sue", lname3 => 'Brown', phone1 => '555-1234-567', ); my @entries; #generate entries from parameters print join "\n", @entries; __END__

        Prints:

        Bloggs,Fred,,fred@xyzzy.com Bloggs,Joe,,joe@plugh.com Brown,Sue,555-1234-567,

        DWIM is Perl's answer to Gödel
        Coming to think of it, scratch that. I can work with having EVERY field required.

        Thanks for your help.

Re: retrieving similar form params
by sulfericacid (Deacon) on Nov 28, 2005 at 00:03 UTC
    Maybe something like this would work.
    my $count = 0; for(1 .. 5) { $count++; if(param("fname$count") ne "") { if(param("lname$count") ne "") { # so on # and so on
    or really, you could test all paramaters in one go instead of separate conditionals.

    Hope this helps.



    "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

    sulfericacid
Re: retrieving similar form params
by kulls (Hermit) on Nov 28, 2005 at 08:56 UTC
    Hi,
    Can you make the html controls (fname,lname,phone,email.,etc) as an array (say same name for all fnam1,fname2 as fname and email1 email2 as email) and in the server-side you can get the params in the array context and do the process. I guess it's better easier.
    Thanx.
    -kulls