473,231 Members | 2,087 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 software developers and data experts.

external hash table

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 5164
eWish
971 Expert 512MB
Please post some code that you have tried. We will help you if you have problems. Meanwhile, please search perldoc,perl.org if you have not written any code and do not understand how.
Nov 8 '07 #2
KevinADC
4,059 Expert 2GB
You can use Data::Dumper for that.

perldoc: Data::Dumper

or Storable:

perldoc: Storable
Nov 8 '07 #3
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 Expert 2GB
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
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 Expert 2GB
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 atggagagaataattctaggcaaggaagacaaaagatatgga
2 tggcccgacaaactttggaagc
3 ggcttacattacattctataattcaagatccaggaatgga
4 atattaactttaatctcacatagcaatctttaatcaatgtgtaaca
5 acgatcgatcgatcgatcgatcgatcgatcgatcgatcgatcgta


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
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 Expert 100+
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 Expert 2GB
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
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
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
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...
2
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...
21
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...
11
by: Douglas Dude | last post by:
how much time does it take to lok for a value - key in a hash_map ? Thanks
139
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
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...
6
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...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

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.