my $count = 0; foreach (@$array) { last if(@$array[$count] =~ m/^\s+.*$/); push (@$array2, @$array[$count]); $count ++; }