in reply to Re^2: Map hashref to get values?
in thread Map hashref to get values?
Yes, but if you use @fields to create your list of column names ($dbfieldlist) for the insert statement, the corresponding values should match, shouldn't they? And hashes have no defined ordering. In other words, if you take the keys from the hash, you'd have to make sure that you sort them appropriately. So why not take @fields directly — I'd suppose it contains the same names as keys %$fields anyway, only in (presumably) different ordering.
my @fields = qw(foo bar baz); # create a hash (as returned by $form->field) my $fields = { map { $_ => "val_$_" } @fields }; my $dbfieldlist = join ", ", @fields; my $dbfielddata = join ", ", map { "'$fields->{$_}'" } keys %$fields; + # wrong ordering #my $dbfielddata = join ", ", map { "'$fields->{$_}'" } @fields; + # ok print "INSERT into DBTABLE ($dbfieldlist) VALUES ($dbfielddata)"; # INSERT into DBTABLE (foo, bar, baz) VALUES ('val_bar', 'val_baz +', 'val_foo')
|
|---|