Hello Monks,

I have created a hash (from a given number of files), which has the following keys : values associated with it:

aw1:10 qs2:20 dd3:30 de4:10 hg5:30 dfd6:20 gf4:20 hgh5:30 hgy3:10 and so on...

There are only 3 values (10,20,30), for this hash. Furthermore, I have a file - "Sample.fa" (with header information) like:

>aw1 ATGCTAGATGCTAGCTAGCTAGCACTGAT CGATGCTAGCGTAGTCAGCTGATGCTGTA CGATGCTAGTCGTACG >qs2 CGAGCTAGTCGTAGTCGTGATGCTGATTA CGATGCTAGTCGTAGCTAGCTGATGCTGC CGATGCTAGTCGTAGTC >dd3 CGTAGTCGTAGTCGTAGTCGATGCTGATG GCTAGTCGATGCTAGCTAGTCGATGCTGG CGATGCTGAT >de4 CGTAGTCGTAGTCGTACGTAGTCGTGAGT CGATTATTTAGGAGGGACAAGGATAGTA >hg5 CGTAGTCGTAGTCTAGTCGTGATGCTAGA >dfd6 CGATGCTACGTACGTAGTCAGTCGTGATG AATTAGAGCAGATAGAGGGGGAAAGGGTT AAACCCC >gf4 CGTAGTCAGTCTAGCTGATGTCGATGCTG >hgh5 CATGCTAGTCGTAGTCGTAGTCGATGCTT TTTTAAGGGAACCCCC >hgy3 CCCCGGGTTTGGGAAAAGGGGGGGGATAG

I want to write the corresponding header and seq from Sample.fa, to 3 files (10.txt, 20.txt, 30.txt -- according to the hash key and value). Following is a snippet from my code :

open (FILE1, ">10.txt"); open (FILE2, ">20.txt"); open (FILE3, ">30.txt"); my @files = glob('Data/*.fa'); my %hash = &readFile(); foreach my $f(@files){ open FILE, $f or die "Cannot open : $!\n"; while (my $line = <FILE>){ chomp $line; #print "$line\n"; if($line =~ /^>/){ my @head = split (/ /, $line); my $name = substr($line, 1); foreach my $key(keys %hash){ if($key eq $name){ if($hash{$key} == 10){ select FILE1; } if($hash{$key} == 20){ select FILE2; } if($hash{$key} == 30){ select FILE3; } } } } } }

Can someone point me in the right direction?

Thanks!


In reply to Compare hash with arrays and print by ad23

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.