my @claim_records = (); while () { push(@claim_records, read_record($_)); } #### ## Put this at the top of your program ## This would be much longer in your program. use constant CLAIM_TEMPLATE => 'A8A6A2'; use constant CLAIM_KEYS => qw( claim_batch_number claim_number claim_type ); #### sub read_record { my ($line) = @_; my %claim_record = (); @claim_record{ CLAIM_KEYS } = unpack(CLAIM_TEMPLATE, $line); return \%claim_record; } #### my $sort_by = 'claim_type'; foreach $record ( sort sub { $a->{$sort_by} cmp $b->{$sort_by} } @claim_records ) { print STDOUT $record->{'claim_number'}, "\n"; }