Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hash of Hashes ( again )

Status
Not open for further replies.

perln00b

Programmer
Oct 4, 2005
21
US
From the last post, first, thank you for your reponses, but I
am still not clear about "hash of hashes". I have one more example here, support,
#########################################################
$statement = "select A, B , C from myTable";
if ($db -> Sql($statement))
{
# ..doing error handling
}
while ($db -> FetchRow())
{
@fields = $db -> Data();
$tempids{$fields[2]}{$fields[0]}{$fields[1]} = 1;
$rev_tempids{$fields[2]}{$fields[1]}{$fields[0]} = 1;
$tempidstrings{$fields[2]}{$fields[0]} .= "$fields[1],";
}
#########################################################
what is the purpose doing that, ie, assigned $tempids{}{}{}=1, ...? can I assign to another
value, instead of 1? In the if-statement, support,
####################################################
if($tempids{A}{C}}
{
# do something
}
###################################################
and what will the $tempids{A}{C} returns?

thanks,
lucas
 
it is a hash of hashes of hashes of integers.
I suggest that you read up on how hashes work, do a google search for Perl data structures cookbook, Tom Christiansen wrote down good examples of different kind of sturctures (hash of lists, lists of hashes etc) in Perl years ago, it is still around.

--
M.Sc. in Computer Science
Freelancing software engineer
visit to contact me regarding software projects
 
Hi,
i have not read your earlier post but let me try to answer this:

Code:
Take this example:
#!/usr/bin/perl

use strict;
use Data::Dumper;

my %h;
$h{A}{B}{C} = 1;

print Data::Dumper::Dumper(\%h);

The output is as follows:
$VAR1 = {
          'A' => {
                   'B' => {
                            'C' => 1
                          }
                 }
        };

So what this means that value of $h{A} is an anonymous hash with a key b whose value is another anynomyous has with a key c which has value 1.

Thus this is equivalent to above too:
Code:
my %h = ( 'A' => { 'B' => { 'C' => 1 } } );
HTH,
San

---
cheers!
san
smoking3vc.gif


print length "The answer to life, universe & everything!
 
what is the purpose doing that, ie, assigned $tempids{}{}{}=1, ...? can I assign to another
value, instead of 1?

why your script is using that specific data structure (hash of hash of hash), I have no clue. I assume it's because it needs to, which I know sounds remarkably glib, but just posting a very small snippet of code from a larger script is not sufficient to know why.

can I assign to another
value, instead of 1? In the if-statement, support,

you can probably assign it any value other than 0 (zero). It looks like a binary flag (1/0) where one is true and zero is false. But maybe not, can't really say for sure for the same reason above: not enough information.
 
133tcamel, thank you so much for your explaination.
Kevin, you bring a very good point -- why your
script is using that specific data structure? This
is only part of the one scripts. "Hash of Hashes"
is used all over the scripts. It is pain in my ass
to fully understand the scripts. I need to trouble-
shoot the scripts and the original code writter was
gone :(. No backup, no documentation. How wonderful!!

Thanks,
Lucas
 
Hi Lucas,

I sympathize with you mate. Reading other peoples code, even if it is a language you know pretty well, is often an excersize in frustration. The bigger and more convoluted the script, the worse the frustration. See if the server error logs can help guide you to where any problem might be occuring in the script.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top