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

This is kind of a newbie question.

I am collecting statistics from a database for data profiling.
I have created a 4 deep hashes within hashes to hold those stats, as such:
$a={NUM_NULL => 0, DISTINCT => 150, NUM_ROWS => 2000}; $b={PRO_NUMBER => \%a}; $c={AC_SHIPMENT => \%b}; %Profileshoh = (IC_STAGE => \%c);

While I have good success obtaining the statistics, I am only getting the last value loaded into the hash structure back when I attempt to print it out.

I suspect my problem is in the loop where I am loading the data. I think I may be re-defining the whole hash at that point, but I can't find an example that shows me what I want to do.

code after readmore
#!/usr/bin/perl -w use DBI; $dbh = DBI->connect("dbi:Oracle:XXXX","XXXXXX","XXXX", { AutoCommit => 0, RaiseError => 1, PrintError => 1 }) or die "can't connect to database:", $DBI::errstr, "\n"; $sth = $dbh->prepare("select 'IC_STAGE', t.table_name, column_name from user_tables t, user_tab_columns c where c.table_name = t.table_name AND t.table_name = 'AC_SHIPMENT' AND column_name in ('ACT_DELIVERY_TSP', 'CONS_NATL_ACCT_NBR', 'PICKUP_UNIT_ID')"); $sth->execute(); while (@row = $sth ->fetchrow_array()) { push @ListoTabCols, [@row]; } $dbh ->disconnect(); print "disconnected\n"; for $i ( 0 .. $#ListoTabCols ) { print "reconnecting\n"; $dbh = DBI->connect("dbi:Oracle:XXXX","XXXX","XXXX", { AutoCommit => 0, RaiseError => 1, PrintError => 1 }) or die "can't connect to database:", $DBI::errstr, "\n"; $prow = $ListoTabCols[$i]; print "row $i : $prow->[0] $prow->[1] $prow->[2]\n"; @returno = $dbh->selectrow_array("select min($prow->[2]) from $pro +w->[1]"); $dbh ->disconnect(); print "disconnected\n"; # HERE IS THE INSERT INTO THE HASH ************* print "returned value: $returno[0]\n"; %Profileshoh = ($prow->[0] => { $prow->[1]=>{ $prow->[2]=>{ MIN_VALUE=>$returno[0] } } }) } while ( ($schemata, $tablas) = each %Profileshoh ) { print "$schemata: "; while ( ($tabla, $colums) = each %$tablas ) { print "$tabla: "; while ( ($colum, $measures) = each %$colums ) { print "$colum: "; while ( ($measure, $value) = each %$measures ) { print "$measure = $value "; } } } print "\n"; } exit;

Replies are listed 'Best First'.
Re: insert data into Nested Hash
by Roy Johnson (Monsignor) on Jul 15, 2004 at 18:12 UTC
    Your hash description is alarming. You do know that \%a is unrelated to hash ref $a, right?

    What you call an insert into the hash is a complete re-assignment of the hash. I think you're making very clunky use of hashes, and perhaps a review of perlreftut would be helpful. At the least, you can change your statement to an insert like so:

    $Profileshoh{$prow->[0]} = { $prow->[1]=>{ $prow->[2]=>{ MIN_VALUE=>$returno[0] } } };
    but I haven't determined whether that's What You Really Want.

    Update: I think you Really Want

    $profileshoh{$prow->[0]}{$prow->[1]}{prow->[2]}{MIN_VALUE} = $returno[ +0];

    We're not really tightening our belts, it just feels that way because we're getting fatter.
      Your hash description is alarming. You do know that \%a is unrelated to hash ref $a, right?

      Whoops! No I didn't realize that. I see now. $a is a reference to an anoymous hash, and \%a is the reference to a named hash, named %a.

      And YES! Your Update was exactly what I really wanted.
      Thank You! :-)
Re: insert data into Nested Hash
by ccn (Vicar) on Jul 15, 2004 at 18:03 UTC
    Please note the difference between:
    $a={NUM_NULL => 0, DISTINCT => 150, NUM_ROWS => 2000}; $b={PRO_NUMBER => $a }; $c={AC_SHIPMENT => $b}; %Profileshoh = (IC_STAGE => $c );

    and
    $a={NUM_NULL => 0, DISTINCT => 150, NUM_ROWS => 2000}; $b={PRO_NUMBER => {%$a} }; $c={AC_SHIPMENT => {%$b}}; %Profileshoh = (IC_STAGE => {%$c} );

    In the first case you share the data between the hashes.
    In the second case each hash have it's own data