my %recordformat = ( '__COMMON__' => [qw( RequestYear 1-2 RequestMonth 3-4 RequestDay 5-6 RequestSequence 7-10 RecordType 11-12 RecordSequence 13-14 )], 94 => [qw( Destination 15-22 DestinationType 23-26 )] # other stuff ); sub fillHashFromData { my ($destination, $recordSpec, $data) = @_; my @spec = @$recordSpec; # make a copy while (@spec) { my $key = shift @spec; my $location = shift @spec; $location =~ /(\d+)(?:-(\d))?/ or die "Syntax error in recordSpec: @$recordSpec"; my ($start, $end) = ($1, $2 || $1); $destination->{$key} = substr($data, $start - 1, $end - $start + 1); } } # Much later.... my $dataLine; while ($dataLine = ) { my %datavalues; fillHashFromData(\%datavalues, $recordformat{__COMMON__}, $dataLine); fillHashFromData(\%datavalues, $recordformat{$datavalues{'RecordType'}}, $dataLine); # Do stuff here }