my %records; my $record; while () { chomp; my $line = $_; s/^(\d\d)//; my $subrecord_type = $1; if ($subrecord_type == 10) { s/^(\d+)\s+//; my $procedure_num = $1; $records{$procedure_num} = $record = []; } push(@$record, $line); } require Data::Dumper; print(Data::Dumper::Dumper(\%records)); __DATA__ 1000001 01.11.199600.00.00001 A1 1 SN Y 2001.11.200400098.0500073.5500083.35 5001.11.1997Professional attendance being an attendance at 5001.11.1997other than consulting rooms, by a general 5001.11.1997practitioner on not more than 1 patient output ====== $VAR1 = { '00001' => [ '1000001 01.11.199600.00.00001 A1 1 SN Y', '2001.11.200400098.0500073.5500083.35', '5001.11.1997Professional attendance being an attendance at', '5001.11.1997other than consulting rooms, by a general', '5001.11.1997practitioner on not more than 1 patient', ] };