473,748 Members | 2,300 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

external hash table

8 New Member
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.
Nov 8 '07 #1
9 5195
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.
Nov 8 '07 #2
KevinADC
4,059 Recognized Expert Specialist
You can use Data::Dumper for that.

perldoc: Data::Dumper

or Storable:

perldoc: Storable
Nov 8 '07 #3
jamietsh
8 New Member
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?

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. use Data::Dumper;
  3.  
  4. $h= do "hash.txt";
  5. %hash=%{$h};
  6.  
  7. for (keys %hash)
  8.     {
  9.         print "$_ => $hash{$_}\n";
  10.     }
  11.  
hash.txt is the external file, and %hash is the imported hash table?

the external file contains this:
Expand|Select|Wrap|Line Numbers
  1. $VAR1 = {
  2. '1' => '1', 
  3. '2' => '2',
  4. '3' => '3',
  5. '4' => '4',
  6.  };
  7.  
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?
Nov 9 '07 #4
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?

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. use Data::Dumper;
  3.  
  4. $h= do "hash.txt";
  5. %hash=%{$h};
  6.  
  7. for (keys %hash)
  8.     {
  9.         print "$_ => $hash{$_}\n";
  10.     }
  11.  
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:
Expand|Select|Wrap|Line Numbers
  1. $VAR1 = {
  2. '1' => '1', 
  3. '2' => '2',
  4. '3' => '3',
  5. '4' => '4',
  6.  };
  7.  
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.
Nov 9 '07 #5
jamietsh
8 New Member
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:
Expand|Select|Wrap|Line Numbers
  1. %DNAsequences = (    
  2. "1",            "atggagagaataattctaggcaaggaagacaaaagatatgga",
  3. "2",            "tggcccgacaaactttggaagc",
  4. "3",            "ggcttacattacattctataattcaagatccaggaatgga",
  5. "4",            "atattaactttaatctcacatagcaatctttaatcaatgtgtaaca",
  6. "5",            "acgatcgatcgatcgatcgatcgatcgatcgatcgatcgatcgta",
  7. );
  8.  
  9. $len1 = int(length(substr($sequence, $k))/1);
  10. for ($l=0;$l<$len1;$l++)
  11. {
  12.     $ans1 = substr($sequence, $k+$l*1,1);
  13.     if(defined($DNAsequences{$ans1}))
  14.     {
  15.         $DNA = $DNAsequences{$ans1};
  16.     }
  17. }
  18.  
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.
Nov 10 '07 #6
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:
Expand|Select|Wrap|Line Numbers
  1. %DNAsequences = (    
  2. "1",            "atggagagaataattctaggcaaggaagacaaaagatatgga",
  3. "2",            "tggcccgacaaactttggaagc",
  4. "3",            "ggcttacattacattctataattcaagatccaggaatgga",
  5. "4",            "atattaactttaatctcacatagcaatctttaatcaatgtgtaaca",
  6. "5",            "acgatcgatcgatcgatcgatcgatcgatcgatcgatcgatcgta",
  7. );
  8.  
  9. $len1 = int(length(substr($sequence, $k))/1);
  10. for ($l=0;$l<$len1;$l++)
  11. {
  12.     $ans1 = substr($sequence, $k+$l*1,1);
  13.     if(defined($DNAsequences{$ans1}))
  14.     {
  15.         $DNA = $DNAsequences{$ans1};
  16.     }
  17. }
  18.  
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

Expand|Select|Wrap|Line Numbers
  1. open(IN, 'hash.txt') or die "$!";
  2. my %DNAsequences = map { chomp; split(/ /); $_[0] => $_[1] } <IN>; 
  3. close IN; 
  4.  
Now you can use %DNAsequences just like you always have in your script.
Nov 10 '07 #7
jamietsh
8 New Member
awsome! it works great thanks alot! =D

can you explain how this line works?

Expand|Select|Wrap|Line Numbers
  1. my %DNAsequences = map { chomp; split(/ /); $_[0] => $_[1] } <IN>;
thanks so much man..
Nov 10 '07 #8
Kelicula
176 Recognized Expert New Member
awsome! it works great thanks alot! =D

can you explain how this line works?

Expand|Select|Wrap|Line Numbers
  1. 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
Nov 10 '07 #9
KevinADC
4,059 Recognized Expert Specialist
awsome! it works great thanks alot! =D

can you explain how this line works?

Expand|Select|Wrap|Line Numbers
  1. my %DNAsequences = map { chomp; split(/ /); $_[0] => $_[1] } <IN>;
thanks so much man..
It's essentially the same as:

Expand|Select|Wrap|Line Numbers
  1. my %DNAsequences;
  2. open(IN, 'hash.txt') or die "$!";
  3. while(<IN>) {
  4.    chomp;
  5.    my($k,$v) = split(/ /);
  6.    $DNAsequences{$k} = $v; 
  7. }
  8. close IN;
Nov 11 '07 #10

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

Similar topics

4
4254
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
34
14474
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:
7
22332
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)....
2
2526
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.
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...
11
2692
by: Douglas Dude | last post by:
how much time does it take to lok for a value - key in a hash_map ? Thanks
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
1
3418
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.
6
10043
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...
0
8991
marktang
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...
0
8831
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,...
1
9326
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
6796
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
4607
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...
0
4877
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3315
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
2
2787
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2215
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.