473,750 Members | 2,557 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

hash of a hash

11 New Member
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
7 2244
nithinpes
410 Recognized Expert Contributor
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']}   ) ;
  4.  
Dec 18 '08 #2
numberwhun
3,509 Recognized Expert Moderator Specialist
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;
  3.  
  4. if($text ~= /^(\w)+\/(\w)+\s+\w+\s+(\w)+\/(\w)+\s+(\w)+)
  5. $hash{$1}{$2} = $5;
  6.  
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.

Regards,

Jeff
Dec 18 '08 #3
cckramer
11 New Member
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/) {
  3.  
  4. # regular expression to get the inputs & outputs
  5.    $line =~ m/\-from\s+\[\s+(.*)\]\s+\-to\s+\[\s+(.*)\]/;
  6.  
  7. # inputs & block information
  8.    $in = $1;
  9.    @in1 = split(/\//, $in);
  10.    $blockin = $in1[0];
  11.    $inport = $in1[1];
  12.  
  13. # outputs & block information
  14.  
  15.    $out = $2;
  16.    @out1 = split(/\//, $out);
  17.    $blockout = $out1[0];
  18.    $outport = $out1[1];
  19.  
  20. # construct Hash of Hash of Hash function
  21.    $HoHoH{$blockin}{$inport}{$outport} = 1;
  22. }
  23. }
  24.  
  25.  
  26. # check if Hash of Hash of Hash has been correctly stored
  27.  
  28. # $blocks gives first level
  29. for my $blocks (keys %HoHoH) {
  30.  
  31. # inputs gives the second level
  32.   for my $inputs (keys %{$HoHoH{$blocks}}) {
  33.  
  34. # outputs gives the third level
  35.    for my $outputs (keys %{$HoHoH{$blocks}{$inputs}}) {
  36.    print "^^ $blocks/$inputs -> $blocks/$outputs ^^\n";
  37. }
  38. }
  39. }
  40.  
Dec 18 '08 #4
numberwhun
3,509 Recognized Expert Moderator Specialist
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.

Regards,

Jeff
Dec 18 '08 #5
cckramer
11 New Member
Thank you.I shall keep that in mind for the future
Dec 18 '08 #6
KevinADC
4,059 Recognized Expert Specialist
@cckramer

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
cckramer
11 New Member
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

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

Similar topics

3
4175
by: Murali | last post by:
I have a requirement where I have to use two unsigned ints as a key in a STL hash map. A couple of ways to do this is 1. create a struct with two unsigned ints and use that as key (write my own HashFcn and EqualKey template args) or, 2. convert the two unsigned ints to char*s, concatenate them and use that as Key. For method 1, the difficulty I am having is in writing the HashFcn. HashFcn requires the following method
2
3790
by: Bryan Olson | last post by:
The current Python standard library provides two cryptographic hash functions: MD5 and SHA-1 . The authors of MD5 originally stated: It is conjectured that it is computationally infeasible to produce two messages having the same message digest. That conjecture is false, as demonstrated by Wang, Feng, Lai and Yu in 2004 . Just recently, Wang, Yu, and Lin showed a short- cut solution for finding collisions in SHA-1 . Their result
24
4307
by: kdotsky | last post by:
Hello, I am using some very large dictionaries with keys that are long strings (urls). For a large dictionary these keys start to take up a significant amount of memory. I do not need access to these keys -- I only need to be able to retrieve the value associated with a certain key, so I do not want to have the keys stored in memory. Could I just hash() the url strings first and use the resulting integer as the key? I think what I'm...
12
7016
by: Arash Partow | last post by:
Hi all, I've ported various hash functions to python if anyone is interested: def RSHash(key): a = 378551 b = 63689 hash = 0
21
3222
by: Johan Tibell | last post by:
I would be grateful if someone had a minute or two to review my hash table implementation. It's not yet commented but hopefully it's short and idiomatic enough to be readable. Some of the code (i.e. the get_hash function) is borrowed from various snippets I found on the net. Thee free function could probably need some love. I have been thinking about having a second linked list of all entries so that the cost of freeing is in proportion to...
21
3903
by: Hallvard B Furuseth | last post by:
Is the code below valid? Generally a value must be accessed through the same type it was stored as, but there is an exception for data stored through a character type. I'm not sure if that applies in this case though: #include <limits.h> unsigned foo(void) { static const union { unsigned char str;
139
14209
by: ravi | last post by:
Hi can anybody tell me that which ds will be best suited to implement a hash table in C/C++ thanx. in advanced
18
1817
by: beginner | last post by:
Hi All. I'd like to do the following in more succint code: if k in b: a=b else: a={} b=a
5
8173
by: Jeff | last post by:
Lets say we have what I would call a "hash": var HASH =new Array(); HASH='first'; HASH='second'; HASH='third'; I'd like to return that as JSON data. Is there a direct way to do that?
1
2974
by: sixtyfootersdude | last post by:
Good Morning! I am a perl newbie and I think that I am struggling with references. I have an array of references to hashes which I am trying to print. This is what I have: for(my $i=0; $i<@input; $i++){ my $hash = $input; print "$i: \n";
0
8838
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9583
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9396
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9342
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6808
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6081
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4888
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2807
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2226
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.