use strict; my (%hash, @G1_out, @G2_out); my @rogues; $hash{$_}=\@G1_out for qw(H0 H1); # etc $hash{$_}=\@G2_out for qw(PX P2); while (<>) { chomp; push @{$hash{substr($_, 0, 2)} || \@rogues}, $_; }