in reply to Efficient Grouping

I'd use more complex data structures which would make it more extensible.

#!/usr/bin/perl -w use strict; use Data::Dumper; my @Def = ( { name => 'Group1', code => [qw( H0 K0 PA PB PC PD PE PF PG PH )] }, { name => 'Group2', code => [qw( PX PY PZ P1 P2 P3 P4 P5 P6 P7 )] }); my %Codes; foreach (@Def) { @Codes{@{$_->{code}}} = ($_->{name}) x @{$_->{code}}; } my %Groups; while (<STDIN>) { chomp; push @{$Groups{$Codes{substr($_, 0, 2)}}}, $_; } print Dumper \%Groups;

You end up with the partitioned data in %Groups. I tested it with this input file:

K0blah PZfoo P7bar PEbaz

which gave the following output:

$VAR1 = { 'Group1' => [ 'K0blah', 'PEbaz' ], 'Group2' => [ 'PZfoo', 'P7bar' ] };
--
<http://www.dave.org.uk>

"The first rule of Perl club is you do not talk about Perl club."
-- Chip Salzenberg