in reply to Re^2: Map hashref to get values?
in thread Map hashref to get values?

In that case, $form->field contains everything you need and you can ignore @fields. Although a hash is unordered, the keys and values functions are guaranteed to return their elements in the same order. So you can do the following, and be confident that things will match up right:

my $fields = join ', ', keys %{$form->field}; my $values = join ', ', map {$dbh->quote($_)} values %{$form->field};

However, as Eliya said, placeholders are generally safer than quote(). To use placeholders, you'll need a count of the values:

my $fields = join ', ', keys %{$form->field}; my @values = values %{$form->field}; my $placeholders = join ',', ('?') x @values; my $sql = qq| INSERT INTO mytable ($fields) VALUES ($placeholders); |; my $st = $dbh->prepare($sql); $st->execute(@values);

Aaron B.
My Woefully Neglected Blog, where I occasionally mention Perl.

Replies are listed 'Best First'.
Re^4: Map hashref to get values?
by apu (Sexton) on Mar 08, 2012 at 04:29 UTC
    Thanks to you both. Had tried with placeholders but was getting the whole $form->field part mixed up so I simplified things. But Aaron's solution worked so thank you.