Fixed Length Text Extract, Write to Excel

From: William Martell (willmartell_at_yahoo.com)
Date: 01/07/04


To: <beginning_perl@yahoogroups.com>,	<beginners@perl.org>
Date: Wed, 7 Jan 2004 08:02:09 -0600

Hello All,

I am trying to work with the code I have to extract fields from a text file report, and write the values into excel.

I am having trouble.

When I get to push @order_detail, %item
I understand that this is pushing an associative array onto a list. (array
of hashes)

I am trying to write code that will open a new work*** in excel and print
the values of %item onto a row. When Perl encounters a certain key
(cust_number), I would like it to start a new row.

I have tried keys(), values(), pop() but I can't get it right. I think some
of my trouble is b/c of the reference \%item. When I remove the reference
backslash operator like this %item and type print. Perl returns with the
contents in scalar.

So. My main question is how do I access the values and keys of %item in
@order_detail???

[code]

#!/perl -w
use strict;
use Data::Dumper;
use Win32::OLE;

############################################################################
#######################
# CODE TO READ FROM FIXED LENGTH TEXT FILE
############################################################################
#######################

my $file = 'Artb30.da4';
open INFILE, $file or die "Can't open $file: $!";

my @order_detail;
while ( <INFILE> ) {
    last if /^GROUP TOTALS/;
    s/\s+\Z//; # remove all trailing whitespace
    next unless /\d\.\d\d\Z/; # only want lines with dollar amounts at end
    my %item;
    if ( length() < 120 ) { # item 1 lines are shorter
        @item{ qw/cust_number cust_name cycle customer_type acct_contact
phone credit_limit/ } =
            map { s/^\s+//; $_ }
            unpack 'A6 x2 A30 x2 A5 x2 A15 x2 A20 x2 A15 x2 A*', $_;
# unpack'A' removes trailing whitespace so we need the map{}
# to remove leading whitespace
        }
    elsif ( /^\d/ ) { # item 2 lines start with a digit
        @item{ qw/inv_no type inv_date current days_1_30 days_31_60
                days_61_90 days_over_90 on_hold unap_cash total_ar/ } =
            map { s/^\s+//; $_ }
            unpack 'A6 x A3 x A8' . 'x3 A11' x 8, $_;
        }
    else { # item 3 lines
        @item{ qw/cust_totals_current cust_totals_days_1_30
cust_totals_days_31_60
                cust_totals_days_61_90 cust_totals_days_over_90
cust_totals_on_hold
                cust_totals_unap_cash cust_totals_total_ar/ } =
            map { s/^\s+//; $_ }
            unpack 'x19' . 'x3 A11' x 8, $_;
        }

    push @order_detail, \%item;

    }

print Dumper \@order_detail;
[/code]

Thanks.

Will Martell
Dallas Texas
-------------------------------------------