if($1 eq 'STOCK NO') { $csv = $2; } $csv .= ",$2"; #### if($1 eq 'STOCK NO') { $csv = $2; } else { $csv .= ",$2"; } #### ($1 eq 'STOCK NO') ? ( $csv = $2 ) : ( $csv .= ",$2" ); #### use strict; use warnings; use Text::CSV; my $file = '782426.pl'; open(my $fh, '<', $file) or die "$file: $!"; my $csv = Text::CSV->new( { eol => "\n" } ); my @columns; foreach ( <$fh> ) { chomp; next unless( m/^([^\.]+)\.+\s+(.*)/ ); push(@columns, $2); if($1 eq 'SALES CST') { # last record of a set $csv->print(\*STDOUT, \@columns); @columns = (); } } close($fh);