I never expected to go from raw data to a .csv file in so few lines of code!
This is one of the reasons so many people like Perl.
Haven't pinned down why the STOCK NO is showing up twice.
My mistake.
if($1 eq 'STOCK NO') { $csv = $2; } $csv .= ",$2";
should have been
if($1 eq 'STOCK NO') { $csv = $2; } else { $csv .= ",$2"; }
or perhaps
($1 eq 'STOCK NO') ? ( $csv = $2 ) : ( $csv .= ",$2" );
Here's an improved version that uses Text::CSV to quote the data and doesn't duplicate the first field.
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);
In reply to Re^5: Text Extraction
by ig
in thread Text Extraction
by sonicscott9041
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |