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.
7 2244
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'. -
%myhash = ( 'blockA' => {'input1'=>['output1','output2']},
-
'blockB' => {'input1'=>['output1']},
-
'blockC' => {'input1'=>['output1'], 'input2'=>['output1']} ) ;
-
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: -
my $text = "blockA/input1 to blockA/output1 delayXX";
-
my $hash;
-
-
if($text ~= /^(\w)+\/(\w)+\s+\w+\s+(\w)+\/(\w)+\s+(\w)+)
-
$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.
Regards,
Jeff
Thanks. I shall give it a shot..also I tried this rough code.Is this okay ? -
# check if line starts with "delay"
-
if ($line =~ /^delay/) {
-
-
# regular expression to get the inputs & outputs
-
$line =~ m/\-from\s+\[\s+(.*)\]\s+\-to\s+\[\s+(.*)\]/;
-
-
# inputs & block information
-
$in = $1;
-
@in1 = split(/\//, $in);
-
$blockin = $in1[0];
-
$inport = $in1[1];
-
-
# outputs & block information
-
-
$out = $2;
-
@out1 = split(/\//, $out);
-
$blockout = $out1[0];
-
$outport = $out1[1];
-
-
# construct Hash of Hash of Hash function
-
$HoHoH{$blockin}{$inport}{$outport} = 1;
-
}
-
}
-
-
-
# check if Hash of Hash of Hash has been correctly stored
-
-
# $blocks gives first level
-
for my $blocks (keys %HoHoH) {
-
-
# inputs gives the second level
-
for my $inputs (keys %{$HoHoH{$blocks}}) {
-
-
# outputs gives the third level
-
for my $outputs (keys %{$HoHoH{$blocks}{$inputs}}) {
-
print "^^ $blocks/$inputs -> $blocks/$outputs ^^\n";
-
}
-
}
-
}
-
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
Thank you.I shall keep that in mind for the future
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?
Thank for all those who helped.My code that I posted earlier to create a hash of hash of hash worked. - (keys{$ blocks{$inputs}{$outputs}})
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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
|
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...
|
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
|
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...
| |
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;
|
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
|
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
|
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?
|
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";
|
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,...
| |
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...
|
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...
|
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,...
|
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...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |