# download the data from the db, using a trapped alarm in case of db # failure (prevents an infinite wait). alarm 600; local $SIG{ALRM} = sub { return $FALSE; }; $dbh -> dbcmd("SELECT * FROM $table"); $dbh -> dbsqlexec; $dbh -> dbresults; # get the column names for the data my @columnames; for (my $i = 0; $i <= $dbh -> dbnumcols; $i++) { push (@columnames, $dbh -> dbcolname($i)); } # write the table data to a hash my %tableData; while (my @data = $dbh -> dbnextrow) { my %tblData; for (my $i = 0; $i < $#columnames; $i++) { $data[$i] =~ s/^\s|\s$//g; $tblData{$columnames[$i+1]} = $data[$i]; } # get the data keys and hash accordingly my $hashkey = $tblData{$key}; delete $tblData{$key}; if (defined $key3) { # if there are 3 keys my $hashkey2 = $tblData{$key2}; my $hashkey3 = $tblData{$key3}; delete $tblData{$key2}; delete $tblData{$key3}; %{$tableData{$hashkey}{$hashkey2}{$hashkey3}} = %tblData; } elsif (defined $key2) { # if there are 2 keys my $hashkey2 = $tblData{$key2}; delete $tblData{$key2}; %{$tableData{$hashkey}{$hashkey2}} = %tblData; } else { # there's only 1 key %{$tableData{$hashkey}} = %tblData; } } return \%tableData;