tags:

views:

409

answers:

4

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++;
}
A: 

My reply on the other thread you had been linking to works. I have tested it and used it many times. It doesn't use execute_array(), just execute().

chris d
I want to insert both keys and values in database and your code does not do that. it is only inserting values.
birdy
When you say 'keys' you mean you want to populate the fields 'sid' and 'sentence' with their values, right? If you're trying to do something else I'm not getting you.. and if this is what you're trying to do any of the methods we've explained should cover it.
chris d
A: 

This should work.

my $key;
my $value;
while (($key, $value) = each %hash) {
    $sth->bind_param(1, $key);
    $sth->bind_param(2, $value);
    $sth->execute();
}
David Harris
It's much better to write that loop as `while( my ( $key, $value ) = each %hash )`
friedo
no still the reference is getting uploaded instead of actual values.
birdy
Print out $key and $value in the loop to see if the values obtainerd from the hash are correct.
David Harris
Also note that I'm using execute not execute_array
David Harris
$key is correct but $value is getting printed as reference value i.e. ARRAY(0x949f6b0). What is going wrong?
birdy
FYI the authoritative reference to Perl DBI is found at <a href="http://search.cpan.org/~timb/DBI/DBI.pm"> </a>
David Harris
@birdy: you have a hash of arrays.
friedo
fyi<code>#!/bin/perluse strict;use warnings;my %hash = ( 1 => 'First Word', 2 => 'Second Word is correct', 0 => 'Third word does not exist', );my $key;my $value;while (my ($key, $value) = each %hash) { print ("key: >$key< value: >$value<\n");}</code>produces<code>key: >1< value: >First Word<key: >0< value: >Third word does not exist<key: >2< value: >Second Word is correct<</code>under cygwin and perl 5.10.0
David Harris
A: 
molecules
it works, but ,as i mentioned, it uploads reference values instead of actual values.
birdy
If we could see more of your code, we may be able to help.
molecules
i added some more part of my code. added how im creating %hash
birdy
+2  A: 

That's not what you originally posted!!! You have a hash of reference to arrays. Read the perl reference tutorial (perlreftut) to learn about them.

(Use the command

perldoc perlreftut

to access this tutorial)

David Harris