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.


—Brad
"The important work of moving the world forward does not wait to be done by perfect men." George Eliot

In reply to Creating a hashes from AoHs by bradcathey

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.