Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Data Structures Help

by hmerrill (Friar)
on Oct 29, 2004 at 12:29 UTC ( [id://403709] : note . print w/replies, xml ) Need Help??


in reply to Data Structures Help

Got it.

Ok, I'm thinking you want a multi-dimensional hash. Here's basically what I'm talking about:

%windows = ( "office 1" => ("120 120" => 3, "140 135" => 1, "155 135" => 1), "bedroom 2" => ("100 75" => 2, "120 180" => 1), );
That is a pre-loaded hash just to show you what the structure will look like. Of course you'll need to load that hash from scratch with your script as you read through the text file.

So in hash %windows, key "office 1" has a value which is a *reference* to an anonymous hash (a *reference* to an anonymous hash is created by the parenthesis), and that reference refers to the anonymous hash which has these key/value's:

Key Value ------- ----- 120 120 3 140 135 1 155 135 1
Then to print out the %windows hash you will need two loops - one loop to iterate through the outer hash(with keys "office 1" and "bedroom 2", and an inner loop to iterate through the hash elements within each of those outer loop elements - something like this:
foreach my($room, $measurements_hashref) (each %windows) { print "$room\n"; print "width\theight\tquantity\n"; foreach my($measurements, $count) (each %$measurements_hashref) +{ print "$measurements $count\n"; } }
***Careful - this is completely untested code, but hopefully it's close.

HTH.