I reversed the hash to get a unique list of the "numbers". Then opened that many files and put the file handles in another hash. This eliminates some of the "if" logic. Using different record separator does help, but a slight bit of fiddling is required.

update: See how different filehandles are used in the print below. Also if what reverse is unclear add a print Dumper(\%rhash); statement.

#!/usr/bin/perl -w use strict; use Data::Dumper; my %hash = (aw1 => 10, qs2 => 20, dd3 => 30, de4 => 10, hg5 => 30, dfd6 => 20, gf4 => 20, hgh5 => 30, hgy3 => 10); my %rhash = reverse %hash; my %fh; foreach my $num (keys %rhash) { open ($fh{"$num.txt"} , '>', "$num.txt") or die ; } $/ = '>'; while (<DATA>) { my $tag = (m/^(\w+)/)[0]; #"blank" rec at beginning next unless defined $tag; s/>$//; my $filename = "$hash{$tag}.txt"; my $handle = $fh{$filename}; print $handle ">$_"; } __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

In reply to Re: Compare hash with arrays and print by Marshall
in thread 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.