I have a hash and I am trying to insert its values into database. Hash is defined as follows:
my %hash = (
1 => 'First Word',
2 => 'Second Word is correct',
0 => 'Third word does not exist',
);
I do not know how to insert values in a database using hashes. I notice my question is similar to this question. But, none of the answers seem to be correct. On using any of the listed answers, the values in hash are not inserted, instead reference to hash is inserted i.e. ARRAY(0x9e63b30)
. But when I print Dumper @values
, values get printed and not reference values.
Any suggestions on how to insert values and not their reference? And, what is going wrong in the solutions listed in answers to question.
@values is defined same as this question i.e.
my @values = values %hash;
Edit: Db structure:
T1:
sid sentence
1 First Word
2 Second Word is correct
0 Third word does not exist
in above sid is keys
of hash and sentence is values
of hash.
this is what I tried out (it is one of the answers to question):
my @keys = keys %hash;
my @values = values %hash;
my $sth = $dbh->prepare("INSERT INTO T1(sid, sentence) VALUES (?,?);");
$sth->execute_array({},\@keys, \@values);
again, while inserting @values
reference values are getting inserted.
EDIT:
_ OUTPUT _
$VAR1 = 'First Word';
$VAR2 = 'Third word does not exist';
$VAR3 = 'Second Word is correct';
_ CODE _ this is how I am inserting values into %hash
my $x=0;
foreach my $file(@files){
if ($file =~ /regex/){
push(@{$hash{$x}}, "$1 $2 $3 $4 $5 $6 $7");
}
elsif ($file =~ /regex/){
push(@{$hash{$x}}, "$1 $2 $3 $4 $5 $6");
}
elseif ($file =~ /Hs_(.+)_(.+)_(.+)_(.+)_(.+)_W.+txt/){
push (@{$hash{$x}}, "$1 $2 $3 $4 $5");
}
$x++;
}