jblapham has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to use a hash and I'm not sure what I'm doing wrong. I believe I'm either populating my has wrong, or printing it wrong. Can someone help point out what my problem (with the code) is?
undef %db_processes; $dbh = DBI->connect("dbi:ODBC:$dsn_name", "$dsn_user", "$dsn_pwd") or quittext_opc ("Failed to connect to database $dsn_name\n", +$app, $SEV1, $GRP3, "5051", $STAT1); $qry = "select sid,'|',serial#,'|',status,'|',process,'|',username,'|' + from v\$session;"; $sth = $dbh->prepare($qry); $sth->execute(); while ( @dummyrow = $sth->fetchrow_array ) { ($pp0,$pp1) = split /:/,$dummyrow[6]; $db_processes{$pp1} = {SID => $dummyrow[0], SERIAL => $dummyrow[2], STATUS => $dummyrow[4], PROCESS => $dummyrow[6], USERNAME => $dummyrow[8], }; } foreach $hashy (@db_processes) { print "in if\n"; print "$hashy->[0]\n"; } $dbh->disconnect();
The data is extracted from the database fine. I have printed the $dummyrows and see the data. I just can't get the hash to print values. Thanks, Jim

Replies are listed 'Best First'.
Re: Hash usage
by blokhead (Monsignor) on Feb 06, 2003 at 16:46 UTC
    You are filling up the hash just fine, but the problem is that you're going about retrieving the data from it all wrong. The hash %db_processes is a different variable than @db_processes the array. In your foreach statement, you are trying to get data out of the array variable (psst, using strict would have pointed this out) which is why you aren't getting anything at all to print.

    To get back data from a hash in a similar way, either use keys, each, or values. If it were me, I'd use each in this case:

    while (my($key, $hashy) = each %db_process) { # same inner loop # also, since $hashy is anticipated to be a HASH reference, # you can't do $hashy->[0], etc. # you must use $hashy->{key} (with the curly, not square brackets) }

    PS: Yay, my 100th post.

    blokhead

      Using strict definately caused me to do a little declaration changing (a lot of my's). However, the print method you suggested doesn't seem to work. This is leading me to believe maybe it isn't getting loaded right.

      As you can see, I am printing the the values for the dummyrow variables. The data looks good. It looks like the right syntax for loading into the hash, but nothing is returned (except "hashy is: ") from the print statement.

      I'm new to hashes, so please don't talk to cryptic.

      undef my %db_processes; my $pp1; my $dbh; my $SEV1; my $app; my $GRP3; my $STAT1; my $pp0; my @dummyrow; my $qry; my $sth; $dbh = DBI->connect("dbi:ODBC:$dsn_name", "$dsn_user", "$dsn_pwd") or quittext_opc ("Failed to connect to database $dsn_name\n", +$app, $SEV1, $GRP3, "5051", $STAT1); $qry = "select sid,'|',serial#,'|',status,'|',process,'|',username +,'|' from v\$session;"; $sth = $dbh->prepare($qry); $sth->execute(); while ( @dummyrow = $sth->fetchrow_array ) { ($pp0,$pp1) = split /:/,$dummyrow[6]; chomp $pp1; print "the pp0 value is: $pp0 and the pp1 value is: $pp1\n"; print "$dummyrow[0] $dummyrow[2] $dummyrow[4] $dummyrow[6] $du +mmyrow[8]\n"; $db_processes{$pp1} = {SID => $dummyrow[0], SERIAL => $dummyrow[2], STATUS => $dummyrow[4], PROCESS => $dummyrow[6], USERNAME => $dummyrow[8], }; } $dbh->disconnect(); while (my($key, $hashy) = each %db_processes) { print "hashy is: $hashy->{key}\n"; }
      In the above print of hashy, I have also tried to reference the hash as $hashy->{$key} with the same results. I was thinking you might have had a typo in your reco.

      Thanks for your help, Jim

        $hashy is a reference to a hash, whose keys are SID, SERIAL, STATUS, PROCESS, and USERNAME. When I wrote $hashy->{key} what I meant was that you should replace "key" with one of the keys of the hash for whatever information you are trying to print, so:
        print "hashy's sid: $hashy->{SID}\n"; # or maybe.. print "hashy's serial: $hashy->{SERIAL}\n"; # etc..
        I didn't mean to use the $key variable that is in that loop ($key actually corresponds to the pp1 value of that record from above -- print it out in the loop as well to see what it is). Sorry if that was a little confusing. You may want to look at perlreftut to understand better the syntax of the hash references you're using.

        blokhead

Re: Hash usage
by davorg (Chancellor) on Feb 06, 2003 at 16:50 UTC

    Putting use strict in your program would have helped you to find the problem. You put your data into %db_processes, but then try to retrieve it from an array called @db_processes which doesn't exist.

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg