in reply to Re: Extract first word from certain lines in a file
in thread Extract first word from certain lines in a file

sub dump_product { my ($product) = @_; print(join(', ', @$product), $/) if (@$product); @$product = (); } my @product; while (<DATA>) { if (/^Product\b/) { dump_product(\@product); } elsif (/^(\S+)/) { push(@product, $1); } } dump_product(\@product); __DATA__ Product: redball This is for Mike. greenball This is for Dave. Product: smallbox This is for apples bigbox This is for orange

output:

redball, greenball smallbox, bigbox