how to format output

M

moonhkt

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");
 
R

rabbits77

moonhkt said:
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 think your question is better phrased as "How do I
choose to send
my format output to something other than STDOUT?"
The answer is to use the select() operator.
Be aware that virtually every other reply you will
get in this thread will be from Asperberger victims
whinging that you shouldn't use format. You may
safely ignore those posts.
 
U

Uri Guttman

r> I think your question is better phrased as "How do I choose to send
r> my format output to something other than STDOUT?"
r> The answer is to use the select() operator.
r> Be aware that virtually every other reply you will
r> get in this thread will be from Asperberger victims
r> whinging that you shouldn't use format. You may
r> safely ignore those posts.

and why would you want to use format? you can use cpan modules (damian's
Text::Format is popular) which are better and easier and allow you to
simply print to any file. perl's builtin format is clunky, inflexible
and even larry wall doesn't use it. so do you have any valid reasons why
the OP should still use format? also larry has aspergerger's. do you
want to insult him some more?

uri
 
R

rabbits77

Uri said:
r> I think your question is better phrased as "How do I choose to send
r> my format output to something other than STDOUT?"
r> The answer is to use the select() operator.
r> Be aware that virtually every other reply you will
r> get in this thread will be from Asperberger victims
r> whinging that you shouldn't use format. You may
r> safely ignore those posts.

and why would you want to use format? you can use cpan modules (damian's
Text::Format is popular) which are better and easier and allow you to
simply print to any file. perl's builtin format is clunky, inflexible
and even larry wall doesn't use it. so do you have any valid reasons why
the OP should still use format?
[purposely obtuse statement snipped]
I believe that instead of blindly following what you or
anyone else has to say the OP should let his own
curiosity lead his exploration of the language.
Sure, he would be well served to be aware of
Text::Format but he would be better off to
know the many different ways perl offers to
accomplish things and then make his own decisions
within the rules defined by his employer, his own
style, and so forth. Clearly the OP is a beginner so
why shouldn't he have some fun exploring all the
neat corners in Perl? By strongly advocating
*don't do this...do that* you are spoiling the very
sense of fun that most people enjoy about Perl to
begin with. Corporations should have coding
standards. Individuals learning a language should
not. Why enforce this stifling corporate mindset?
What is clunky and inflexible are the coding rules
you are trying to force onto other people. Perl is
a language that allows for a great deal more
creativity in expression than you want to allow
others to have. What an odd mentality. This is like
bringing a hungry child to an ice cream shop and
only allowing them to have one flavor of ice cream.
Perl Best Practices should be read after one knows
the language very well and not one second before
that!
 
U

Uri Guttman

r> I believe that instead of blindly following what you or anyone else
r> has to say the OP should let his own curiosity lead his exploration
r> of the language. Sure, he would be well served to be aware of
r> Text::Format but he would be better off to know the many different
r> ways perl offers to accomplish things and then make his own
r> decisions within the rules defined by his employer, his own style,
r> and so forth. Clearly the OP is a beginner so why shouldn't he have
r> some fun exploring all the neat corners in Perl? By strongly

consider that the 'neat' corner he asked about is poorly done and very
few perl hackers use it. forewarning newbies about a bad corner is perl
is a good thing. learning to use modules is a very good thing. so the OP
can use this as a double learning opportunity, learning to use cpan and
also learning how to use a decent module for formatting text.

r> advocating *don't do this...do that* you are spoiling the very
r> sense of fun that most people enjoy about Perl to begin
r> with. Corporations should have coding standards. Individuals

who said anything about coding standards? you are babbling here.

r> learning a language should not. Why enforce this stifling corporate
r> mindset? What is clunky and inflexible are the coding rules you

who said anything about corporate mindsets? i don't work for any
corporation but i have taught plenty of perl to many different levels of
perl hackers.

r> are trying to force onto other people. Perl is a language that
r> allows for a great deal more creativity in expression than you want
r> to allow others to have. What an odd mentality. This is like
r> bringing a hungry child to an ice cream shop and only allowing them
r> to have one flavor of ice cream. Perl Best Practices should be
r> read after one knows the language very well and not one second
r> before that!

who mentioned perl best practices? i mentioned one module that damian
wrote. the fact that he wrote it wasn't even important other than
knowing it will be solid and well documented which are big plusses.

so you imply i mentioned coding standards, corporate mindsets and perl
best practices when i said none of them. i should say you are the one
with delusions of how perl should be used and taught, not me.

now please reply with another inane post which assumes more things
unsaid by me. i order you to do this, now!

uri
 
R

rabbits77

Uri said:
r> I believe that instead of blindly following what you or anyone else
r> has to say the OP should let his own curiosity lead his exploration
r> of the language. Sure, he would be well served to be aware of
r> Text::Format but he would be better off to know the many different
r> ways perl offers to accomplish things and then make his own
r> decisions within the rules defined by his employer, his own style,
r> and so forth. Clearly the OP is a beginner so why shouldn't he have
r> some fun exploring all the neat corners in Perl? By strongly

consider that the 'neat' corner he asked about is poorly done and very
few perl hackers use it. forewarning newbies about a bad corner is perl
is a good thing. learning to use modules is a very good thing. so the OP
can use this as a double learning opportunity, learning to use cpan and
also learning how to use a decent module for formatting text.

r> advocating *don't do this...do that* you are spoiling the very
r> sense of fun that most people enjoy about Perl to begin
r> with. Corporations should have coding standards. Individuals

who said anything about coding standards?
Call your admonitions what you will. Labeling them
as "rules" or "standards", no matter the source,
still apply. You are clearly trying to back out of
your statements. Good!
To the OP: use format. Try things
out and explore! Have fun!
Give back to the community by not developing this
annoying preachy attitude about how to do things
either.
 
U

Uri Guttman

r> advocating *don't do this...do that* you are spoiling the very
r> sense of fun that most people enjoy about Perl to begin
r> with. Corporations should have coding standards. Individuals

how nice that you obeyed my order to reply but deleted it from the
quote!

r> Call your admonitions what you will. Labeling them as "rules" or
r> "standards", no matter the source, still apply. You are clearly trying
r> to back out of your statements. Good!
r> To the OP: use format. Try things
r> out and explore! Have fun!
r> Give back to the community by not developing this
r> annoying preachy attitude about how to do things
r> either.

me thinks thou dost protest too much. i smell preachy all over you. i
don't preach, i teach. i offer suggestions, better ideas, solutions,
etc. readers or students are free to do as they please after i
pontficate from above. you are obviously wiser than me so please train
all the newbies you can. are you on the perl beginner's list?

and i repeat my order to inanely reply to this. you will keep replying
until i tell you to stop. this is fun!

uri
 
R

RedGrittyBrick

moonhkt said:
Hi All

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


I find reading the Perl documentation helps. The 'format' statement is
used in conjunction with 'write' not with 'print' so read `perldoc -f
write` and note the `write filehandle` syntax.

Note that format/write are somewhat deprecated/superseded.

I've added some other suggestions below on coding style.

I *always* add
use strict;
use warnings;
because I find then extremely useful in avoiding errors.
$fn="io.txt"; # Input file
$fout = ">io2.txt"; # Write to file

It's better not to mix the io directives with the filenames. See
`perldoc -f open` and note the three-argument form of open.
$abc="String";
open ( OUTFILE, $fout);

Modern idiom for this is
open my $outfile, '>', $fout or die "can't open '$fout' because $!";
open( IN, $fn) or die (" cannot open in.txt for reading: $!");

You got the file name wrong in the error message!
open my $in, '<', $fn or die "Can't write to '$fn' because $!";
format HEADER=
@<<<<<<<<<@<<@<<
$abc$so_type$code

.

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

($abc, $so_type, $code) = split ... ;

write $outfile;
}

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


close ( IN ) or die("Cannot close file: $!");

close $in or die "Can't close $fn because $!";
close (OUTFILE) or die ("Can not close io2.txt");

close $outfile or die "Can't close $fout because $!";

But read `perldoc -f close` for ways to check for success. Sometimes
you'll need to include $? in the error message.


Code suggestions untested.
 
R

rabbits77

moonhkt said:
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");
 
R

rabbits77

rabbits77 said:
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.
Oh, also, I will explicitly note that you need not
use the defaults!
You would change
$FORMAT_NAME(same thing as $~)

and/or

$FORMAT_TOP_NAME(same thing as $^)

A great tutorial and reference for formats is R.
Schwartz's Learning Perl(1st or 2nd edition).
From the third edition onwards formats are not
covered.
If things like $~ and $^ make you giddy than read
more on built in variables.
http://linux.die.net/man/1/perlvar
 
P

Peter Makholm

rabbits77 said:
A great tutorial and reference for formats is R.
Schwartz's Learning Perl(1st or 2nd edition).
From the third edition onwards formats are not
covered.

I wonder why?

//Makholm
 
U

Uri Guttman

BM> Formats are considered deprecated nowadays. There are many better
BM> alternatives on CPAN. If you want the format-style fixed-width picture
BM> approach, I rather like Perl6::Form (which, despite the name, is a
BM> perfectly straightforward Perl 5 module).

peter was being sarcastic to rabbit who didn't know why formats weren't
covered in more recent editions of learning perl. :) rabbit keeps
defending them regardless of the real world moving away from them. maybe
he should still use perl4?

uri
 
C

Charlton Wilbur

TJM> perl 4 is a wonderful language!

TJM> Doesn't have all that confusing "software engineering" stuff
TJM> that you boys like so much.

Don't *do* that.

Charlton
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top