in reply to split file

The following should get you started:

use warnings; use strict; my %bills; my $currBill = 'void'; my $type = 'header'; while (<DATA>) { chomp; if (/^1010100\.+(\d+)/) { $currBill = $1; $type = 'header'; } elsif (/^3010330\.+(\d+)/) { $currBill = $1; $type = 'data'; } else { push @{$bills{$currBill}{$type}}, $_; } } for my $bill (sort keys %bills) { print ">>>> $bill\n"; print "Header\n" . (join "\n", @{$bills{$bill}{'header'}}) . "\n" if exists $bills{$bill}{'header'}; print "Data\n" . (join "\n", @{$bills{$bill}{'data'}}) . "\n" if exists $bills{$bill}{'data'}; } __DATA__ 1010100.........123456..... #header line for bill # 123456 #some data #some data #some data #some data 1010100.........678910..... #header line for bill # 678910 #some data #some data #some data 3010330.........123456..... #bill detail for bill 123456 #some data #some data #some data #some data #some data 3010330.........678910..... #bill detail for bill 678910 data for 678910
>>>> 123456 Header #some data #some data #some data #some data Data #some data #some data #some data #some data #some data >>>> 678910 Header #some data #some data #some data Data data for 678910

Open your files in the for loop, print their contents as required and then close.


DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^2: split file
by Anonymous Monk on Feb 10, 2006 at 04:51 UTC
    When I ran this script, the output was actually the same as the DATA file. In addition, it results in the following warning: useless use of a variable in void context at sp_new_load_script.pl line 6 (referring to my %bills;) I can't quite figure out where it's going wrong. Any ideas? Thanks.

      I just copied the code as posted and ran it using AS Perl 5.8.7 (815). It performed as expected. You could try adding use diagnostics; at the start of the code - it may give you more information about the warning.


      DWIM is Perl's answer to Gödel
        Thanks for your help so far. I've got through the previous error and this code works fine with the data I've posted. I made some changes to reflect the actual data, but I now have the same problem where the output doesn't show up in the right order.
        This's my actual data file: I have spent much time on this but I still cannot have it working properly. Please help.