jdtoronto has asked for the wisdom of the Perl Monks concerning the following question:
The two arrays are returned by a sub that allows the user to select an available field from the database for each field of the import.
The number of fields in the import can vary! The input db's can have as few as 5 or as many as 30 fields and the order of the fields varies also.
In the past I wrote a thing like this for one particular flat-file layout, I parsed the fields directly into a hash using split:
Then after a couple of other simple operations I took the array of hashes and stored it like this:sub Read_leads { open (INPUT, "<$lo_in") || die "Cannot open source file $!\n"; while (<INPUT>) { $in_ln = $_; $in_ln =~ s/^\s+//; $in_ln =~ s/\s+$//; ($lead{Email}, $lead{FirstName}, $lead{LastName} , $lead{DOB}, + $lead{Gender}, $lead{Country}, $lead{State}, $lead{ZIP}, $lead{Addre +ss}, $lead{City}) = split(/,/, $in_ln); $lead{DATE} = $indate; if ($lead{Email}) { $lead{Datasource} = '10222'; $rec = {}; while (($k, $v) = each %lead) { $v = $dbh->quote( $v); $rec->{$k} = $v; } push @leadslist, $rec; } %lead = (); } return; }
But how big can the array of hashes get? OK, I can do the import line by line and play it safe, thats no problem.sub Write_leads { @fields = qw(Email FirstName LastName Address City State ZIP Datas +ource AdType OrigIP DATE Gender DOB phone Country); $sth = $dbh->prepare("INSERT INTO $leads_tbl VALUES (?,?,?,?,?,?,? +,?,?,?,?,?,?,?,?)"); for $href (@leadslist) { $sth->execute( @{$href}{@fields}); } return; }
But how do I handle the variable nature of what I am trying to do now?
Many thanks in anticipation!
John
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: DBI and two arrays question!
by dragonchild (Archbishop) on Jul 24, 2003 at 18:41 UTC | |
|
Re: DBI and two arrays question!
by tadman (Prior) on Jul 24, 2003 at 18:45 UTC | |
|
Re: DBI and two arrays question!
by graff (Chancellor) on Jul 25, 2003 at 02:07 UTC |