use strict; use warnings; use Data::Dumper; my ($i, @d) = (-1); while (<>) { if (/^Dut No/) { $d[++$i] = ''; } else { $d[$i] .= $_; } } print Dumper \@d;