By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,834 Members | 2,234 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,834 IT Pros & Developers. It's quick & easy.

hash of a hash

P: 11
I have this info' in a file.

blockA/input1 to block A/output1 delayXX
blockA/input1 to blockA/output2 delayYY
blockA/input1 to blockA/output3 delayZZ

blockB/input1 to blockB/output1 delayPP

blockC/input1 to blockC/output1 delayQQ
blockC/input2 to blockC/output1 delayRR

how to store the inputs and outputs for each block as shown above ? in 2 hashes or arrays ? I am not interested in the delay shown.I just need to capture the possible combination of inputs and outputs for each block.
Dec 18 '08 #1
Share this Question
Share on Google+
7 Replies

Expert 100+
P: 410
You can probably create a hash with block names as keys. Let the value for each key be an anonymous hash in which inputs are keys and the possible outputs for each input(stored in anonymous array) as values.
You end up in the following structure which is 'hash of hash of an array'.
Expand|Select|Wrap|Line Numbers
  1. %myhash = ( 'blockA' => {'input1'=>['output1','output2']},
  2.                      'blockB' => {'input1'=>['output1']},
  3.                       'blockC' => {'input1'=>['output1'], 'input2'=>['output1']}   ) ;
Dec 18 '08 #2

Expert Mod 2.5K+
P: 3,503
Being the late hour that it is I just don't have it in me (mentally) to produce full code at the moment, but what I can say is that you can first use a regular expression to parse out of each line what you are needing as far as text. Then, store it in the hash, as such:

Expand|Select|Wrap|Line Numbers
  1. my $text = "blockA/input1 to blockA/output1 delayXX";
  2. my $hash;
  4. if($text ~= /^(\w)+\/(\w)+\s+\w+\s+(\w)+\/(\w)+\s+(\w)+)
  5. $hash{$1}{$2} = $5;
There are a few different ways to handle hashes of hashes, but at work we tend to use something similar to this and it works great. Granted, at this late hour I may have something incorrect in the assignment and also, it is untested, but you should get the idea.

Also, search google for hash of hashes and you may find some good links.


Dec 18 '08 #3

P: 11
Thanks. I shall give it a shot..also I tried this rough code.Is this okay ?
Expand|Select|Wrap|Line Numbers
  1. # check if line starts with "delay"
  2.  if ($line =~ /^delay/) {
  4. # regular expression to get the inputs & outputs
  5.    $line =~ m/\-from\s+\[\s+(.*)\]\s+\-to\s+\[\s+(.*)\]/;
  7. # inputs & block information
  8.    $in = $1;
  9.    @in1 = split(/\//, $in);
  10.    $blockin = $in1[0];
  11.    $inport = $in1[1];
  13. # outputs & block information
  15.    $out = $2;
  16.    @out1 = split(/\//, $out);
  17.    $blockout = $out1[0];
  18.    $outport = $out1[1];
  20. # construct Hash of Hash of Hash function
  21.    $HoHoH{$blockin}{$inport}{$outport} = 1;
  22. }
  23. }
  26. # check if Hash of Hash of Hash has been correctly stored
  28. # $blocks gives first level
  29. for my $blocks (keys %HoHoH) {
  31. # inputs gives the second level
  32.   for my $inputs (keys %{$HoHoH{$blocks}}) {
  34. # outputs gives the third level
  35.    for my $outputs (keys %{$HoHoH{$blocks}{$inputs}}) {
  36.    print "^^ $blocks/$inputs -> $blocks/$outputs ^^\n";
  37. }
  38. }
  39. }
Dec 18 '08 #4

Expert Mod 2.5K+
P: 3,503
You need to please use code tags when posting code. If you notice, the rest of us use them and when you don't, we have to clean up behind you.

This is your nice warning.


Dec 18 '08 #5

P: 11
Thank you.I shall keep that in mind for the future
Dec 18 '08 #6

Expert 2.5K+
P: 4,059

Hard to say because the data your code is parsing is not the same as the sample data you posted. On the face of it, the code looks OK to me. But I think a hash of arrays might work better. Whats the actual data look like you are parsing?
Dec 18 '08 #7

P: 11
Thank for all those who helped.My code that I posted earlier to create a hash of hash of hash worked.
Expand|Select|Wrap|Line Numbers
  1. (keys{$ blocks{$inputs}{$outputs}})
Dec 19 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.