To just use it for STDOUT, it's pretty straight forward, switching the
special $~ variable as needed.

You're switching FORMATs not filehandles.  There may be a shortcut but this
is how I would do it for this problem:

-Tom Kinzer

_____________________

format TYPE_1 =
Im formated with type1: @<<<<<<<<<  @||||||||||||||||||||||||||||||||||||||
$field1, $field2
.

format TYPE_2 =
IM FORMATTED WITH TYPE2: @<<<<<<<<  @<<<<<<<<<<<<<<<<<<<<<<<<
$field1, $field2
.

open RECORDS, "< YourInput.log" or die;

while (<RECORDS>) {

   ($field1, $field2)  = split;

   if ( m/YourPatternRegExHere/ ) {

           $~ = 'TYPE_1';

   } else {

       $~ = 'TYPE_2';

   }

   write;

}
close RECORDS;

__END__

-----Original Message-----
From: Jose Malacara [mailto:[EMAIL PROTECTED]
Sent: Friday, December 05, 2003 6:40 PM
To: [EMAIL PROTECTED]
Subject: formats


Is it possible to use more than one format in a script? I am parsing a log
file to STDOUT and would like to be able to write to two (or more) different
formats depending on the information found in the log.

This is what I'm trying to do:

1. open and read logfile
2. if you find "some_string" print to STDOUT using TYPE_1 format
3. if you find "other_string" print to STDOUT using TYPE_2 format
5. continue parsing logfile, etc

I can open and parse the logfile, my script breaks whenever I try to call
more than one type of format. This doesn't seem to work for me, is this even
possible?


format TYPE_1 =
Type: @<<<<<<<<<<  Gateway: @<<<<<<<<<  Acct ID: @<<<<<<<<<<<<<<<<<<
$record_type,$gateway,$acct_id
Start Date: @<<<<<<<<<<  Start Time: @<<<<<<<<<
$start_date,$start_time
.
write TYPE_1;

format TYPE_2 =
Type: @<<<<<<<<<<  Gateway: @<<<<<<<<<  Acct ID: @<<<<<<<<<<<<<<<<<<
$record_type,$gateway,$acct_id
Start Date: @<<<<<<<<<<  Start Time: @<<<<<<<<<
$start_date,$start_time
Disconnect Date: @<<<<<<<<<<  Disconnect Time: @<<<<<<<<<
$disco_date,$disco_time
.
write TYPE_2;


Also, how would I go about keeping the filehandles open as it would be
repetively "writing" each format.


Thank you,
Jose


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to