Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

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
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 (Priest) 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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (9)
As of 2014-12-23 03:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (135 votes), past polls