my (@w, $c); local ( $", $_ ) = ", "; do { $_ = ; if ( defined $_ && !/^Product:/ ) { push @w,(split ' ')[0]; } elsif ( @w ) { print "Product: ", ++$c, "\n@w\n"; @w = (); } } while ( defined $_ ); __DATA__ Product: redball This is for Mike. greenball This is for Dave. Product: smallbox This is for apples bigbox This is for orange