hi,
i would like to store my hash table in an external file and call it in my perl script.
how should i store my external hash table and what is the syntax to read it?
thanks alot.
9 5193 eWish 971
Recognized Expert Contributor
Please post some code that you have tried. We will help you if you have problems. Meanwhile, please search perldoc,perl.or g if you have not written any code and do not understand how.
thanks for the options. i've tried using data dumper and this is a sample i got off the net, it works but i do not understand how..
anyome care to explain it to me? -
#!/usr/bin/perl
-
use Data::Dumper;
-
-
$h= do "hash.txt";
-
%hash=%{$h};
-
-
for (keys %hash)
-
{
-
print "$_ => $hash{$_}\n";
-
}
-
hash.txt is the external file, and %hash is the imported hash table?
the external file contains this: -
$VAR1 = {
-
'1' => '1',
-
'2' => '2',
-
'3' => '3',
-
'4' => '4',
-
};
-
how to i write it such that i can store 2 coulmns of data in the external table and can use a variable to compare it to the table, if it matches a data in the first coulmn then it will print out the data in the corresponding column?
KevinADC 4,059
Recognized Expert Specialist
thanks for the options. i've tried using data dumper and this is a sample i got off the net, it works but i do not understand how..
anyome care to explain it to me? -
#!/usr/bin/perl
-
use Data::Dumper;
-
-
$h= do "hash.txt";
-
%hash=%{$h};
-
-
for (keys %hash)
-
{
-
print "$_ => $hash{$_}\n";
-
}
-
hash.txt is the external file, and %hash is the imported hash table?
Correct, hash.txt is the external file, and %hash is the imported hash.
the external file contains this: -
$VAR1 = {
-
'1' => '1',
-
'2' => '2',
-
'3' => '3',
-
'4' => '4',
-
};
-
how to i write it such that i can store 2 coulmns of data in the external table and can use a variable to compare it to the table, if it matches a data in the first coulmn then it will print out the data in the corresponding column?
The code you posted is using the "do" function to import the hash from the file. Data::Dumper is not being used to do anything at all in that snippet of code. The hash in the file is actually a reference to a hash. You can tell because it starts with '$' instead of '%' and uses curly braces {} instead of parenthesis ().
Your question at the end I don't understand.
maybe this will help with my question. this is my origianl program that i have developed, but the hash table is getting bigger and bigger, therefore i would like to put the hash table in an external file.
my original has table IN the program itself: -
%DNAsequences = (
-
"1", "atggagagaataattctaggcaaggaagacaaaagatatgga",
-
"2", "tggcccgacaaactttggaagc",
-
"3", "ggcttacattacattctataattcaagatccaggaatgga",
-
"4", "atattaactttaatctcacatagcaatctttaatcaatgtgtaaca",
-
"5", "acgatcgatcgatcgatcgatcgatcgatcgatcgatcgatcgta",
-
);
-
-
$len1 = int(length(substr($sequence, $k))/1);
-
for ($l=0;$l<$len1;$l++)
-
{
-
$ans1 = substr($sequence, $k+$l*1,1);
-
if(defined($DNAsequences{$ans1}))
-
{
-
$DNA = $DNAsequences{$ans1};
-
}
-
}
-
the "actgatcga" are DNA sequences.
$sequence is the choice of 1-5 from the GUI page. then the loops will look for the which number it gets from the GUI page and will then assign the DNA strand from the corresponding number according to the hash table to $DNA.
so i guess what i mean is that i want the function to be the same just that the hash table is not in the program itself.
KevinADC 4,059
Recognized Expert Specialist
maybe this will help with my question. this is my origianl program that i have developed, but the hash table is getting bigger and bigger, therefore i would like to put the hash table in an external file.
my original has table IN the program itself: -
%DNAsequences = (
-
"1", "atggagagaataattctaggcaaggaagacaaaagatatgga",
-
"2", "tggcccgacaaactttggaagc",
-
"3", "ggcttacattacattctataattcaagatccaggaatgga",
-
"4", "atattaactttaatctcacatagcaatctttaatcaatgtgtaaca",
-
"5", "acgatcgatcgatcgatcgatcgatcgatcgatcgatcgatcgta",
-
);
-
-
$len1 = int(length(substr($sequence, $k))/1);
-
for ($l=0;$l<$len1;$l++)
-
{
-
$ans1 = substr($sequence, $k+$l*1,1);
-
if(defined($DNAsequences{$ans1}))
-
{
-
$DNA = $DNAsequences{$ans1};
-
}
-
}
-
the "actgatcga" are DNA sequences.
$sequence is the choice of 1-5 from the GUI page. then the loops will look for the which number it gets from the GUI page and will then assign the DNA strand from the corresponding number according to the hash table to $DNA.
so i guess what i mean is that i want the function to be the same just that the hash table is not in the program itself.
External file (hash.txt).
File format: A number followed by a single space followed by dna data:
1 atggagagaataatt ctaggcaaggaagac aaaagatatgga
2 tggcccgacaaactt tggaagc
3 ggcttacattacatt ctataattcaagatc caggaatgga
4 atattaactttaatc tcacatagcaatctt taatcaatgtgtaac a
5 acgatcgatcgatcg atcgatcgatcgatc gatcgatcgatcgta
Your perl script -
open(IN, 'hash.txt') or die "$!";
-
my %DNAsequences = map { chomp; split(/ /); $_[0] => $_[1] } <IN>;
-
close IN;
-
Now you can use %DNAsequences just like you always have in your script.
awsome! it works great thanks alot! =D
can you explain how this line works? - my %DNAsequences = map { chomp; split(/ /); $_[0] => $_[1] } <IN>;
thanks so much man..
Kelicula 176
Recognized Expert New Member
awsome! it works great thanks alot! =D
can you explain how this line works? - my %DNAsequences = map { chomp; split(/ /); $_[0] => $_[1] } <IN>;
thanks so much man..
It grabs a line from the <IN> file and splits the data into two separate entities on the "space" character, then makes the first one (ie: the number) a key , and the letters gacgacag etc... the value, and puts all that into the Hash %DNAsequences, then does the same thing again for the next line, thus populating the hash from the file.
There is a great module for constructing Multi level database files, and constructs. MLDBM , however the aforementioned Data::Dumper, is VERY useful. It actually saves to file the perl code needed to "recreate" the same constructs, and or files again.
Saving you a lot of work!
Read more: http://search.cpan.org/~chamas/MLDBM-2.01/lib/MLDBM.pm http://search.cpan.org/~ilyam/Data-D....121/Dumper.pm KevinADC 4,059
Recognized Expert Specialist
awsome! it works great thanks alot! =D
can you explain how this line works? - my %DNAsequences = map { chomp; split(/ /); $_[0] => $_[1] } <IN>;
thanks so much man..
It's essentially the same as: - my %DNAsequences;
-
open(IN, 'hash.txt') or die "$!";
-
while(<IN>) {
-
chomp;
-
my($k,$v) = split(/ /);
-
$DNAsequences{$k} = $v;
-
}
-
close IN;
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Pelo GANDO |
last post by:
Hi everybody !
I am a beginner in C++. I am looking for a (simple if it's possible) source
code in C++ about hash table...
Thank you !
Pelo
|
by: pembed2003 |
last post by:
Hi All,
Does C++/STL have hashtable where I can do stuff like:
Hashtable h<int>;
h.store("one",1);
h.store("two",2);
and then later retrieve them like:
|
by: Matthias Käppler |
last post by:
Hi,
I need to store objects of a FileInfo class containing information about a
specific file. I can uniquely identify these files by their serial number.
I thought it would be a good idea to use a hash map so I can access the
file information in constant time, without having to iterate over a
(possibly very large) list of files.
As far as I know, std::map does not hash its entries (I guess it takes
O(nlogn) time to find an entry)....
|
by: Ravi |
last post by:
Hi,
I am working on a winform app. I need to use an object which can store
some information(key/value pairs) and also can be acessed by multiple
threads(read/write). From what I heard Hash table is best suited for it. My
question is
Why hash table. why can't we use an array.
I thought hash table uses more memory than array. Correct me if am wrong.
|
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: Douglas Dude |
last post by:
how much time does it take to lok for a value - key in a hash_map ?
Thanks
|
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: jacob navia |
last post by:
Everybody knows that hash tables are fast.
What is less known is that perfect hash tables are even faster.
A perfect hash table has a hash function and a table layout that
avoids collisions completely. You can lookup a key in the table
with a single hash function call.
What is even less known is that there is a perfect software
for generating perfect hash tables called "gperf" offered by
GNU INC.
|
by: j1mb0jay |
last post by:
I am currently working on a dictionary populating program. I currently
have a socket connection my local news server and am trawling through
all of the articles looking for new words. I am currently using Java to
do this but would like to move the source to C#. Java's String class has
a method that hashes strings. I was wondering if C# has a method which
does the same?
In my Java version of the program I am using the Multiply Add and...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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: 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: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |