in reply to Compare hash with arrays and print

By setting $/ appropriately, you could read in entire records, which would make your life somewhat easier.

Also, there is no need to iterate over all keys of the hash to then just pick the one where $key eq $name:

foreach my $key(keys %hash){ if($key eq $name){ if($hash{$key} == 10){ ...

A direct lookup $hash{$name} would serve the same purpose.

Here's a simplified demo using __DATA__:

#!/usr/bin/perl open (FILE1, ">10.txt") or die $!; open (FILE2, ">20.txt") or die $!; open (FILE3, ">30.txt") or die $!; my %hash = (aw1 => 10, qs2 => 20, dd3 => 30, de4 => 10, hg5 => 30, dfd6 => 20, gf4 => 20, hgh5 => 30, hgy3 => 10); { local $/ = "\n>"; # input record separator while (<DATA>){ s/^>//mg; my ($name) = /^([^\n]+)/; if($hash{$name} == 10){ select FILE1; } if($hash{$name} == 20){ select FILE2; } if($hash{$name} == 30){ select FILE3; } print ">$_"; } } __DATA__ >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

(just re-add the looping over your @files)

Replies are listed 'Best First'.
Re^2: Compare hash with arrays and print
by ad23 (Acolyte) on Jul 12, 2010 at 19:28 UTC

    Thanks for your reply!

    I am still not able to print anything to my files.

    my %hash = &readFile(); my @fasta = glob('Data_Test/*.fa'); foreach my $f (@fasta){ open FILE, $f or die "Cannot open $fastaname for reading: $!\n"; local $/ = "\n>"; while (<FILE>) { s/^>//mg; my ($name) = /^(\w+)/; if($hash{$name} == 10){ select FILE1; } if($hash{$name} == 20){ select FILE2; } if($hash{$name} == 30){ select FILE3; } print ">$_"; } } close(FILE1); close(FILE2); close(FILE3);

      Add some debugging prints (print STDERR ... preferably, as you're manipulating the default output file handle) to figure out what's different from the sample I've given (which does work fine for me).

      For example, print the records ($_), $name, the corresponding hash values $hash{$name}, etc.  Are they what you'd expect?

        I added some print statements, and now I am able to get the required contents in the output files. Thanks a lot!

        Also, I have some mixed fasta header data. For eg:

        >aw1.a1 bhi|tn|56564 pairs:40098 ATGCTAGATGCTAGCTAGCTAGCACTGAT CGATGCTAGCGTAGTCAGCTGATGCTGTA CGATGCTAGTCGTACG >aw1.b1 bhi|tn|56565 pairs:40099 CGAGCTAGTCGTAGTCGTGATGCTGATTA CGATGCTAGTCGTAGCTAGCTGATGCTGC CGATGCTAGTCGTAGTC >dd3.a1 bhi|tn|56566 pairs:40100 CGTAGTCGTAGTCGTAGTCGATGCTGATG GCTAGTCGATGCTAGCTAGTCGATGCTGG CGATGCTGAT >dd3.b1 bhi|tn|56567 pairs:40101 CGTAGTCGTAGTCGTACGTAGTCGTGAGT CGATTATTTAGGAGGGACAAGGATAGTA >hg5.a1 bhi|tn|56568 pairs:40102 CGTAGTCGTAGTCTAGTCGTGATGCTAGA >dfd6.a1 bhi|tn|56569 pairs:40103 CGATGCTACGTACGTAGTCAGTCGTGATG AATTAGAGCAGATAGAGGGGGAAAGGGTT AAACCCC >ght5.a1 bhi|tn|56564 ATGCTAGTCGTAGTCGATGTCGTAGCTGT CGTAGCTGATGCATGCTAGTCCGTAGCTG >tgt6.X bhi|tn|56564 pairs:56478 CGTAGCTGATGCTGATGCTGATGCTGTGT CGTAGCTGATGCTGATGCTTAGCTGATGC CGTAGCTGATCGTAGCTATCGTAGCTAGG >tgt6.Y bhi|tn|56564 pairs:56479 CGTAGTCGTAGTCGTAGTCGATGCTAGTG CGATGCTGATCGTGATGCTATGCTAGCGT CAGTCGTAGTCGTACGTAGTCGTGTGTGG

        I want to write the complete header line (starting with '>') in my output files.

        Can I use the split function to divide the header line into an array and then try printing it?

        Thanks!

      update: oops I do see that you do have a print statement. and looks like it should work.

      This "select FILE3;" statement by itself does nothing useful. I actually wouldn't use select at all in this situation.

      The Perl print statement is a "smart" critter. If the first arg of print is the file handle of some open file, Perl will print to that file handle. print FILE3 "abc"; will print "abc" to FILE3. The Perl default is essentially "print stdout "abc";". Select makes the default print go to wherever you want (instead of stdout), but here it appears easier to just put the file handle in the print statement.

      print FILE3 $_; or similar will work fine. A plain "print;" sends $_ to the default file handle. With a file handle specified, I think you have to explicitly say $_ for the same effect.