#!/usr/bin/perl use strict; use warnings; my @array = ( 'Dut No', 'asdf', 'asdf', 'asdf', 'Dut No', 'yxcv', 'yxcv', 'yxcv', 'yxcv', 'Dut No', 'fhgj', 'fhgj', 'fhgj'); my $count = 0; my $in_dut_no = undef; foreach my $item (@array){ if ($item eq "Dut No"){ $in_dut_no = 1; $count++; print "\nDut No ($count)\n"; } elsif ($in_dut_no){ print "$item\n"; } }