in reply to retrieving similar form params

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

Replies are listed 'Best First'.
Re^2: retrieving similar form params
by Anonymous Monk on Nov 27, 2005 at 23:24 UTC
    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
        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>

      Coming to think of it, scratch that. I can work with having EVERY field required.

      Thanks for your help.