my $section = ''; #remember the last section label we encountered foreach (@m) { if ($_ =~ /^Dig No\s:\s(\w*)\s*Prior:\s*([0-9]*)\s*Digstrt:\s*([0-9]{2}\/[0-9]{2}\/[0-9]{2})\s*Time:\s*([0-9]{2}:[0-9]{2})/) { $section = 'Dig No'; $m{'DIG_NO' } = $1; $m{'PRIORITY'} = $2; $m{'DIGDATE' } = $3; $m{'DIGTIME' } = $4; } elsif ($_ =~ /^Address\s*:\s*(.*)Subdivsn/) { $section = 'Address'; $m{'ADDRESS' } = $1; } elsif ($_ =~ /^Remarks\s*:\s*(.*)/ || $section eq 'Remarks') { #do this if we enter the section or were already in the section $section = 'Remarks'; $m{'REMARKS' } = $1; } }