I am trying to optimize a utility which groups an incoming stream into (currently) six groups, based on the first two letters. What I have now looks like:
my @Group1 = qw( H0 K0 PA PB PC PD PE PF PG PH ); #etc my @Group2 = qw( PX PY PZ P1 P2 P3 P4 P5 P6 P7 ); #etc # etc, 6 lists defined, with 100-200 prefixes each foreach my $code (@Group1) { $G1_Hash{$code} = 1; } foreach my $code (@Group2) { $G2_Hash{$code} = 1; } while (my $input = <STDIN>) { chomp ($input); my $prefix = substr($input,0,2); push (@G1_out, $input) if $G1_Hash{$prefix}; push (@G2_out, $input) if $G2_Hash{$prefix}; }
I know there has to be a more efficient solution to this. Maybe a way to define the hash in one step, instead of populating the list first?

In reply to Efficient Grouping by meetraz

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.