Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

poj's scratchpad

by poj (Prior)
on Jun 12, 2013 at 17:16 UTC ( #1038520=scratchpad: print w/replies, xml ) Need Help??

This creates an AoA (Array of Arrays), it only works if the records in your input file follow a regular pattern and text does not get spread across onto a new page

#!perl use strict; #use Data::Dump 'pp'; use Text::CSV; my $infile = 'report.txt'; my $outfile = 'report.csv'; # report structure in DATA my @header =(); my @colname =(); while (<DATA>){ chomp; if (/^-(.*)/){ push @header,[$1]; } else { push @colname,$_; } } #pp \@header,\@colname; my ($n,$i); my @data =(); my @rec =(); # parse file in paragraph mode $/ = ""; open IN,'<',$infile or die "$!"; while (<IN>) { chomp; next if /Page \d of \d/; if ($n < @header){ $header[$n][1] = $_; ++$n; } else { push @rec,$_; ++$i; if ($i > $#colname){ push @data,[@rec]; @rec=(); $i = 0; } } }; close IN; #pp \@header; # create csv my $csv = Text::CSV->new ( { binary => 1 , eol => "\n", } ) or die "Cannot use CSV: ".Text::CSV->error_diag (); open my $fh, '>', $outfile or die "$outfile : $!"; $csv->print($fh, $_) for @header; for my $rec (@data){ $csv->print($fh, $rec); } close $fh or die "$outfile : $!"; # report template # -fields are only on page 1 __DATA__ -COMPANY -REPORT_DATE -REPORT_TITLE Permit Issued Address Description

Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (9)
As of 2017-03-30 15:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (360 votes). Check out past polls.