[boulder.pm] hash for multiple filehandles?

Suppes, Patrick pls at bighorn.dr.lucent.com
Thu Jun 29 13:53:17 CDT 2000


Here is a fragment of a program that processes a file with multiple record
types.
It's overkill for what you asked for, but it combines the functionality of
splitting the
file with doing something unique with each of the different record types.

First it defines the parsing for each record type...
#
#   Header record.
$splitpattern{"A"} = "A1 A6 A6 A6 A8 A6 A6 A32 A50 A32 A2 A4 A5 A3 A1 A8 A6 A10
A825 ";
#   Detail record.
$splitpattern{"D"} =
    # Segment ID
        "A1 A6 A8 " ...
    # More fields
        "A1 A9 A10 A10 A11 A16 A50 A94 A2 A40 A60";
#   Trailer record.
$splitpattern{"Z"} = "A1 A10 A16 A17 A10 A963";

Then it reads the records from the file...

while ($record=<STDIN>) {
   chomp $record;

   $recordtype=substr($record, 0, 1);
   $fieldsizes=$splitpattern{$recordtype};
   @fields=unpack($fieldsizes, $record);

... actually do something useful by record type,
   in this case it did some field level editing of the input file,

 then writes out the data by record type.

	 print $recordtype "@fields\n";

};


This program only uses 3 record types, so the open and close statements are hard
coded.
In your case you may want to cycle through opening and closing the various
output files
using the for each loop.
  (The following code was not tested....)

	foreach $recordtype ( keys %splitpattern ) {open $recordtype,
">$Out.$recordtype";}

and, later

	foreach $recordtype ( keys %splitpattern ) {close $recordtype;}

Pat Suppes



> -----Original Message-----
> From:	Robert L. Harris [SMTP:Robert.L.Harris at rnd-consulting.com]
> Sent:	Thursday, June 29, 2000 11:54 AM
> To:	Boulder Perl Mongers
> Subject:	[boulder.pm] hash for multiple filehandles?
> 
> 
> 
> Ok,
>   I have a very large file.  it has about 30 record types.  We currently 
> use about 15 of those.  Right now, I open 15 filehandles, manually, 
> have 15 print statements, and 15 closes.  There has got to be a
> better way.
> 
>     open(OUT01, ">$Out.01") || die "Can't open Outputfile $Out.01 :$!:\n";
>     open(OUT26, ">$Out.26") || die "Can't open Outputfile $Out.26 :$!:\n";
>     open(OUT36, ">$Out.36") || die "Can't open Outputfile $Out.36 :$!:\n";
>     open(OUT37, ">$Out.37") || die "Can't open Outputfile $Out.37 :$!:\n";
>     while(<INFILE>) {
>       # do some processing
>       .
>       .
>       .
> 
>       print OUT01 "$Line\n" if ($RecNum eq "01");
>       print OUT26 "$Line\n" if ($RecNum eq "26");
>       print OUT36 "$Line\n" if ($RecNum eq "36");
>       print OUT37 "$Line\n" if ($RecNum eq "37");
>     }
>     close(OUT01);
>     close(OUT26);
>     close(OUT36);
>     close(OUT37);
> 
> 
> 
> :wq!
> ---------------------------------------------------------------------------
> Robert L. Harris                |  Micros~1 :  
> Senior System Engineer          |    For when quality, reliability 
>   at RnD Consulting             |      and security just aren't
>                                 \_       that important!
> DISCLAIMER:
>       These are MY OPINIONS ALONE.  I speak for no-one else.
> FYI:
>  perl -e 'print $i=pack(c5,(41*2),sqrt(7056),(unpack(c,H)-2),oct(115),10);'



More information about the Boulder-pm mailing list