in reply to Creating a hashes from AoHs

very close .. @fields is an array of hash refs, so you have to dereference it as a hash with the % ...
for ( 0 .. $#fields) { %sql_data = %{$fields[$_]}; ... insert into DB ... }
or really just (more 'perlish' loop):
foreach my $sql_data ( @fields ) { $stmt = qq/INSERT INTO temp_sheet (/ . join(',', keys %$sql_data ) +. qq/) VALUES (/ . join(',', ('?') x keys %$sql_data ) . qq/)/ +; $dbh->do( $sql, {}, values %$sql_data ); }

A quick aside -- an example using SQL::Abstract:
use SQL::Abstract; my $sql = SQL::Abstract->new; foreach my $data (@fields){ my($stmt, @bind) = $sql->insert('temp_sheet', $data); $dbh->do( $stmt, {}, @bind ); }

Replies are listed 'Best First'.
Re^2: Creating a hashes from AoHs
by bradcathey (Prior) on Mar 03, 2006 at 03:35 UTC

    Thanks davidrw, great answers and examples.

    Strange though, I did try

    %sql_data = %{$fields[$_]};

    before my OP, but for some reason it didn't work. Anyway, I'm good to go...and a little smarter.


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