Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

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; } } }

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
[LanX]: hmm interesting link
[Discipulus]: Eduardo De Filippo explains how to do coffe - ITA

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2017-01-16 11:08 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (149 votes). Check out past polls.