Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Using Varibale for HAsh name

by shawshankred (Sexton)
on Oct 22, 2010 at 16:02 UTC ( #866827=perlquestion: print w/ replies, xml ) Need Help??
shawshankred has asked for the wisdom of the Perl Monks concerning the following question:

Perl experts.
I have a hash called tab2hash that has
tablenames as key and hashnames as value. I want to
initialize the hashes with the values from DB tables. And I
have the code below, however when i print the hashes
they are empty. This line is where the hash is being populated

$$VALUE_1{$k1} = $v1;

Please help with your suggestions.
my %tab2hash = qw(divisions divH regions regH); sub initHashesArraysFromDB { my ($k1, $v1); while ((my $KEY_1, my $VALUE_1) = each %tab2hash) { my $sth = $dbh->prepare("select * from $KEY_1"); $sth->execute || die "Cannot execute query: " .DBI->errstr; while (my @row = $sth->fetchrow()) { my $f1 = 0; foreach my $col (@row) { if($f1 == 0) { $f1 = 1; $k1 = $col; $v1 = $col; } else { $v1 .= "|" . $col; } } $$VALUE_1{$k1} = $v1; } } }

Comment on Using Varibale for HAsh name
Download Code
Replies are listed 'Best First'.
Re: Using Varibale for HAsh name
by Corion (Pope) on Oct 22, 2010 at 16:36 UTC
Re: Using Varibale for HAsh name
by SuicideJunkie (Vicar) on Oct 22, 2010 at 16:38 UTC

    As above, Don't.

    Try a HoH instead:

    my %divisionsHash; my %regionsHash; my %hashes = (divisions=>\%divisionsHash, regions=>\%regionsHash); # Access it ala: $hashes{'regions'}{'North America'}; $hashes{'divisions'}{'Finance'};

      Thanks. But that does not solve my problem. I want to populate the hashes with the data in the DB tables in one shot.

        See DBI for ->fetchrow_arrayref and the Slice => {} parameter.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://866827]
Approved by moritz
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2015-11-28 19:51 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (744 votes), past polls