in reply to Re: retrieving similar form params
in thread retrieving similar form params

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.

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

    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
Re^3: retrieving similar form params
by GrandFather (Saint) on Nov 27, 2005 at 23:35 UTC

    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
      Hi.

      Using your data structure, this is what I aim to do.

      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', ); # assuming everything above passes the sanity check my $stored = qq(INSERT INTO addbook (fname, lname, phone, address, ema +il) VALUES(?,?,?,?,?); my $sth = $dbh->prepare($data); $sth->execute($fname#, $lname#, $phone#, $address#, $email#) or die $d +bh->errstr;
      Each row of fields has to be entered into my database like that.

      Thank you. </code>

Re^3: retrieving similar form params
by Anonymous Monk on Nov 27, 2005 at 23:27 UTC
    Coming to think of it, scratch that. I can work with having EVERY field required.

    Thanks for your help.