Re: how to format output



moonhkt wrote:
Hi All

How to format output ? I want output format in io2.txt file.


$fn="io.txt"; # Input file
$fout = ">io2.txt"; # Write to file
$abc="String";
open ( OUTFILE, $fout);
open( IN, $fn) or die (" cannot open in.txt for reading: $!");

format HEADER=
@<<<<<<<<<@<<@<<
$abc$so_type$code

.

while (my $line = <IN>) {
print "$line";
}

print (OUTFILE "Output to file\n");


close ( IN ) or die("Cannot close file: $!");
close (OUTFILE) or die ("Can not close io2.txt");

I have only minimally changed your code. I have purposely tried to touch what you wrote
as little as possible.
If you elaborate on what you want to do I could help
more.

The default naming convention for a format is that it match the filehandle.
If you want the format HEADER to really be the header the default naming convention for that is
to match the format followed by _TOP. In your case
OUTFILE_TOP.

$fn="io.txt"; # Input file
$fout = ">io2.txt"; # Write to file
$abc="String"; # Make sure all your variables
my $line; # used in the format are scoped
# correctly!
open(OUTFILE, $fout);
open(IN, $fn) or die (" cannot open in.txt for reading: $!");

format OUTFILE_TOP=
@<<<<<<<<<@<<@<<
$abc$so_type$code
..

format OUTFILE=
@<<
$line
..

select(OUTFILE);

while ($line = <IN>) {
write;
}

print (OUTFILE "Output to file\n");


close ( IN ) or die("Cannot close file: $!");
close (OUTFILE) or die ("Can not close io2.txt");


.