Just to add my $0.02. I didn't use a hash at all, I used
grep.
use strict;
my @Group1 = qw( H0 K0 PA PB PC PD PE PF PG PH );
my @Group2 = qw( PX PY PZ P1 P2 P3 P4 P5 P6 P7 );
my @G1_out;
my @G2_out;
while (my $input = <DATA>) {
chomp ($input);
my $prefix = substr($input,0,2);
# NB: grep is slow in this case. evil. beware.
push (@G1_out, $input) if grep($_ eq $prefix, @Group1);
push (@G2_out, $input) if grep($_ eq $prefix, @Group2);
}
print "G1\n";
print "$_\n" foreach @G1_out;
print "\nG2\n";
print "$_\n" foreach @G2_out;
__DATA__
A1 # invalid
K0 # valid G1
B4 # invalid
PY # valid G2
Gives:
G1
K0 # valid G1
G2
PY # valid G2
Update: I guess i should've mentioned that i knew it was slower. The bonus I saw is that it doesn't require a hash per group, which i think is a good thing. I guess my priorities lie elsewhere =) My bad.
--
Rock is dead. Long live paper and scissors!
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.