469,306 Members | 2,398 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,306 developers. It's quick & easy.

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 4963
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

Post your reply

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

Similar topics

4 posts views Thread by Pelo GANDO | last post: by
34 posts views Thread by pembed2003 | last post: by
7 posts views Thread by Matthias Käppler | last post: by
2 posts views Thread by Ravi | last post: by
21 posts views Thread by Johan Tibell | last post: by
11 posts views Thread by Douglas Dude | last post: by
139 posts views Thread by ravi | last post: by
1 post views Thread by jacob navia | last post: by
6 posts views Thread by j1mb0jay | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.