external hash table

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jamietsh
    New Member
    • Jul 2007
    • 8

    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.
  • eWish
    Recognized Expert Contributor
    • Jul 2007
    • 973

    #2
    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.

    Comment

    • KevinADC
      Recognized Expert Specialist
      • Jan 2007
      • 4092

      #3
      You can use Data::Dumper for that.

      perldoc: Data::Dumper

      or Storable:

      perldoc: Storable

      Comment

      • jamietsh
        New Member
        • Jul 2007
        • 8

        #4
        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?

        Code:
        #!/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:
        Code:
        $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?

        Comment

        • KevinADC
          Recognized Expert Specialist
          • Jan 2007
          • 4092

          #5
          Originally posted by jamietsh
          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?

          Code:
          #!/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:
          Code:
          $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.

          Comment

          • jamietsh
            New Member
            • Jul 2007
            • 8

            #6
            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:
            Code:
            %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.

            Comment

            • KevinADC
              Recognized Expert Specialist
              • Jan 2007
              • 4092

              #7
              Originally posted by jamietsh
              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:
              Code:
              %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

              [code=perl]
              open(IN, 'hash.txt') or die "$!";
              my %DNAsequences = map { chomp; split(/ /); $_[0] => $_[1] } <IN>;
              close IN;
              [/code]

              Now you can use %DNAsequences just like you always have in your script.

              Comment

              • jamietsh
                New Member
                • Jul 2007
                • 8

                #8
                awsome! it works great thanks alot! =D

                can you explain how this line works?

                Code:
                my %DNAsequences = map { chomp; split(/ /); $_[0] => $_[1] } <IN>;
                thanks so much man..

                Comment

                • Kelicula
                  Recognized Expert New Member
                  • Jul 2007
                  • 176

                  #9
                  Originally posted by jamietsh
                  awsome! it works great thanks alot! =D

                  can you explain how this line works?

                  Code:
                  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:

                  Comment

                  • KevinADC
                    Recognized Expert Specialist
                    • Jan 2007
                    • 4092

                    #10
                    Originally posted by jamietsh
                    awsome! it works great thanks alot! =D

                    can you explain how this line works?

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

                    Code:
                    my %DNAsequences;
                    open(IN, 'hash.txt') or die "$!";
                    while(<IN>) {
                       chomp;
                       my($k,$v) = split(/ /);
                       $DNAsequences{$k} = $v; 
                    }
                    close IN;

                    Comment

                    Working...