bradcathey has asked for the wisdom of the Perl Monks concerning the following question:
Fellow Monastians:
As I pull in records from a flat, tab-delimited file, I parse it into an AoH, one array element for each record (I have to go through this preliminary step because I have to test a few of the hash values for legitimacy).
In the end, I loop through the array and copy each of the hashes into a single hash for insertion into my DB table.
I thought I could do this in just one step:
for ( 0 .. $#fields) { %sql_data = $fields[$_]; ... insert into DB ... }
but even with Data Dumping I couldn't figure out why it wasn't working. So, as a work-around until I could post it here, I did a very convoluted:
for ( 0 .. $#fields) { $sql_data{'description'} = $fields[$_]{'description'}; $sql_data{'billing_code'} = $fields[$_]{'billing_code'}; $sql_data{'user_id'} = $fields[$_]{'user_id'}; $sql_data{'project_id'} = $fields[$_]{'project_id'}; $sql_data{'bad_proj'} = $fields[$_]{'bad_proj'}; $sql_data{'bad_bill'} = $fields[$_]{'bad_bill'}; $sql_data{'bad_user'} = $fields[$_]{'bad_user'}; $stmt = qq/INSERT INTO temp_sheet (/ . join(',', keys %sql_data ) . + qq/) VALUES (/ . join(',', ('?') x keys %sql_data ) . qq/)/; + $sth = $dbh->prepare($stmt); $sth->execute(values %sql_data ); }
I know this could be better, but I just can't make it work. What am I totally not seeing. Thanks in advance.
Yes, I'm using strict.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Creating a hashes from AoHs
by davidrw (Prior) on Mar 03, 2006 at 02:52 UTC | |
by bradcathey (Prior) on Mar 03, 2006 at 03:35 UTC | |
|
Re: Creating a hashes from AoHs
by graff (Chancellor) on Mar 03, 2006 at 08:26 UTC | |
by bradcathey (Prior) on Mar 03, 2006 at 12:49 UTC | |
by graff (Chancellor) on Mar 04, 2006 at 00:45 UTC | |
|
Re: Creating a hashes from AoHs
by radiantmatrix (Parson) on Mar 03, 2006 at 14:44 UTC |