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 5020
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.
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?
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.
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 atggagagaataattctaggcaaggaagacaaaagatatgga
2 tggcccgacaaactttggaagc
3 ggcttacattacattctataattcaagatccaggaatgga
4 atattaactttaatctcacatagcaatctttaatcaatgtgtaaca
5 acgatcgatcgatcgatcgatcgatcgatcgatcgatcgatcgta
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..
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
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;
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
| | | | | | | | | | |