Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > how to format output

Reply
Thread Tools

how to format output

 
 
moonhkt
Guest
Posts: n/a
 
      07-06-2009
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");

 
Reply With Quote
 
 
 
 
rabbits77
Guest
Posts: n/a
 
      07-06-2009
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 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.


 
Reply With Quote
 
 
 
 
Uri Guttman
Guest
Posts: n/a
 
      07-06-2009
>>>>> "r" == rabbits77 <(E-Mail Removed)> writes:

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

--
Uri Guttman ------ http://www.velocityreviews.com/forums/(E-Mail Removed) -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
 
Reply With Quote
 
rabbits77
Guest
Posts: n/a
 
      07-06-2009
Uri Guttman wrote:
>>>>>> "r" == rabbits77 <(E-Mail Removed)> writes:

>
> 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!

 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      07-06-2009
>>>>> "r" == rabbits77 <(E-Mail Removed)> writes:

r> Uri Guttman wrote:
>>>>>>> "r" == rabbits77 <(E-Mail Removed)> writes:

>>
>> 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?


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

--
Uri Guttman ------ (E-Mail Removed) -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
 
Reply With Quote
 
rabbits77
Guest
Posts: n/a
 
      07-06-2009
Uri Guttman wrote:
>>>>>> "r" == rabbits77 <(E-Mail Removed)> writes:

>
> r> Uri Guttman wrote:
> >>>>>>> "r" == rabbits77 <(E-Mail Removed)> writes:
> >>
> >> 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?

>
> 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.

 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      07-06-2009
>>>>> "r" == rabbits77 <(E-Mail Removed)> writes:

>> 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

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

>> who said anything about coding standards?


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

--
Uri Guttman ------ (E-Mail Removed) -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
 
Reply With Quote
 
RedGrittyBrick
Guest
Posts: n/a
 
      07-07-2009

moonhkt wrote:
> 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.

--
RGB
 
Reply With Quote
 
rabbits77
Guest
Posts: n/a
 
      07-07-2009
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");


 
Reply With Quote
 
rabbits77
Guest
Posts: n/a
 
      07-07-2009
rabbits77 wrote:
> 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.

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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Want help on how we convert output to tabular format Using the expat parser (http://expat.sourceforge.net/) i have to parse the following xml file and print it on the screen in tabular format. sharan XML 1 10-26-2007 01:20 PM
Sony Precision Cinema Progressive Output vs Component 480p Output Otto Pylot DVD Video 1 04-18-2004 09:49 PM
Is Fuji S3000 3.2m/pixel output, or 6 m/pixel interpolated output? Peter H Digital Photography 43 12-04-2003 02:35 PM
Output / Debug window output bug? John Bentley ASP .Net 0 09-10-2003 07:38 AM



Advertisments