Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Converting a fixed-width report to an Excel-friendly format

by Corion (Patriarch)
on Oct 25, 2006 at 16:00 UTC ( [id://580613]=note: print w/replies, xml ) Need Help??


in reply to Converting a fixed-width report to an Excel-friendly format

I've used the practice you outline to process human-list-reports into nice, machine-treatable reports to great success, except without the slurping part:

package Parser::ReportFoo; use strict; sub new { ... $self->columns = [qw(date customer amount)]; }; my @handlers = [ # Line containing uninteresting information [qr/^Line containing uninteresting information/ => \&discard], [qr/^------------/ => \&discard], [qr/^(\d\d-[A-Z]{3}-\d\d) (..........) (\d+\.\d+)/ => \&captur +e_transaction], [qr/^\s*$/ => \&flush ], [qr/^ ) (..........)\s+$/ => \&capture_name2], ]; sub discard {}; sub flush { my ($self) = @_; my @row = map { $self->$_ } (@{ $self->columns }); print join "\t", @row; }; sub capture_transaction { my ($self,$date,$customer,$amount) = @_; $self->date($date); $self->customer($customer); $self->amount($amount); }; sub capture_name2 { my ($self,$customer) = @_; $self->customer($self->customer . " " . $customer); $self->flush(); }; sub parse { my ($self,$file) = @_; my $fh = open "<", $file or die "$file: $!"; while (defined my $line = <$fh>) { # First, check which regex my $handled; for (@handlers) { my ($re,$code) = @$_; if (my @match = ($line =~ /$re/)) { $code->($self, @match); $handled = 1; last; }; }; warn "Unhandled line >>$line<<"; }; };

The separation of flush() and capture_foo is because I have sometimes reports where one logical row spans several lines or where the transaction date is noted at the top of the "page", so some information has to persist before a whole line can be printed to the results.

I use tab separated files with a file extension of .xls which is the most Excel friendly thing you can get without involving Win32::OLE or SpreadSheet::WriteExcel.

Update: monarch spotted a typo/error in sub parse, I was using @_ where it should have been @$_.

Replies are listed 'Best First'.
Re^2: Converting a fixed-width report to an Excel-friendly format
by flake (Initiate) on Oct 26, 2006 at 17:30 UTC
    My excel here recognizes files with extention of .tab by default as text with tab seperated values (it's also my fav delimited format) :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://580613]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2024-04-23 08:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found