Fixed Length Text Extract, Write to Excel

Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default Fixed Length Text Extract, Write to Excel

    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 worksheet 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;
    Thanks.

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



    William Martell Guest

  2. Similar Questions and Discussions

    1. #38327 [Asn->Bgs]: False notice: lookbehind assertion is not fixed length
      ID: 38327 Updated by: nlopess@php.net Reported By: php dot net at bokehman dot com -Status: Assigned +Status: ...
    2. How to extract multilingual to MS Excel
      How can I extract multilingual data into MS Excel application. At the moment the data extracted into Excel file is not readable. Currently using...
    3. Extract Data from Excel-files
      Hello everyone, Finally I get to use perl at work! =) I am to facing the following problem: There is a folder on a file-server in our network...
    4. Best way to query a flat fixed length text file
      I am trying to insert a semi fixed length file into a SQL database. Does anyone know of a way to query a fixed length file like you can a csv to...
    5. sql*loader fixed width column / variable length record
      im trying to figure this out. i have a datafile with the following records tony sometext 120121122 mark sometext2 120 ethan ...
  3. #2

    Default Re: Fixed Length Text Extract, Write to Excel

    William Martell wrote:
    > 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)
    That is the problem, a list cannot contain a hash. The keys and values of the hash simply become flattened into list
    elements, and all the hashing magic is lost.

    perldoc perlref

    will explain how references can help resolve this problem..

    Joseph


    R. Joseph Newton Guest

  4. #3

    Default Re: Fixed Length Text Extract, Write to Excel

    Thanks for your reply Joseph, I am reading perlref now. If you can offer
    some pointers on how to access the key value pairs of the reference object
    %item. I would appreciate it.


    ----- Original Message -----
    From: "R. Joseph Newton" <rjnewton@efn.org>
    To: "William Martell" <willmartell@yahoo.com>
    Cc: <beginning_perl@yahoogroups.com>; <beginners@perl.org>
    Sent: Wednesday, January 07, 2004 11:20 AM
    Subject: Re: Fixed Length Text Extract, Write to Excel

    > William Martell wrote:
    >
    > > 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)
    >
    > That is the problem, a list cannot contain a hash. The keys and values
    of the hash simply become flattened into list
    > elements, and all the hashing magic is lost.
    >
    > perldoc perlref
    >
    > will explain how references can help resolve this problem..
    >
    > Joseph
    >
    >
    >
    > --
    > To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
    > For additional commands, e-mail: [email]beginners-help@perl.org[/email]
    > <http://learn.perl.org/> <http://learn.perl.org/first-response>
    >
    William Martell Guest

  5. #4

    Default Re: Fixed Length Text Extract, Write to Excel

    William Martell wrote:
    >
    > 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
    Looking at your code, you mean

    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 worksheet 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???
    Hi William.

    You're building an array of hash references. To access a hash values
    from a given array element, write

    my $cust_number = $order_detail[0]{cust_number}

    To do the same for each detail record, do this

    foreach my $detail (@order_detail) {
    my $cust_number = $detail->{cust_number};
    }

    The value returned will be 'undef' if there is no such element for a given
    order detail record.

    I hope this helps.

    Rob


    Rob Dixon Guest

  6. #5

    Default Re: Fixed Length Text Extract, Write to Excel

    Please bottom post...
    > Thanks for your reply Joseph, I am reading perlref now. If you can offer
    > some pointers on how to access the key value pairs of the reference object
    > %item. I would appreciate it.
    >
    May I also suggest the little easier reading of,

    perldoc perlreftut
    perldoc perldsc
    perldoc perllol

    To go along with, and usually (at least the first) before perlref. It
    can be quite thick to try and get through. The above are designed to be
    less of a complete reference and more of a real world help tool....

    Good luck,

    [url]http://danconia.org[/url]
    Wiggins D Anconia Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139