Hey all,

I'm writing a fairly nasty piece of code atm which will read in a table from a database (using Sybase::DBlib) and hash it based on user-provided keys (it has to be done this way - I need to read a lot of tables and need the reusability).

I am storing each row's data as it comes from the DB in a temporary hash keyed on column name, and want to append this to the master hash keyed on the specified columns (there could currently be up to 5 keys).

My problem comes when appending the data to the master hash - because the number of keys is unknown, I can think of no "clean" way to append the data. I have experimented a little with recursion, to no avail, and am left currently with long and ugly code which copes with up to 3 keys - which isn't enough. Is there an "easier" way to place this data into the hash?

Any suggestions would be appreciated.

My code atm is as follows:

# 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}} = %tblDa +ta; } 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;

I'm sure there must be a better way to do this, but I really can't see it.

Thanks -- Foxcub.


In reply to Recursive hash assignment by Tanalis

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.