Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to format output

0 views
Skip to first unread message

moonhkt

unread,
Jul 6, 2009, 11:14:55 AM7/6/09
to
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");

rabbits77

unread,
Jul 6, 2009, 12:21:44 PM7/6/09
to
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.


Uri Guttman

unread,
Jul 6, 2009, 12:43:28 PM7/6/09
to
>>>>> "r" == rabbits77 <rabb...@my-deja.com> 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 ------ u...@stemsystems.com -------- 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 ---------

rabbits77

unread,
Jul 6, 2009, 1:16:17 PM7/6/09
to
Uri Guttman wrote:
>>>>>> "r" == rabbits77 <rabb...@my-deja.com> 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!

Uri Guttman

unread,
Jul 6, 2009, 1:30:31 PM7/6/09
to
>>>>> "r" == rabbits77 <rabb...@my-deja.com> writes:

r> Uri Guttman wrote:
>>>>>>> "r" == rabbits77 <rabb...@my-deja.com> 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!

rabbits77

unread,
Jul 6, 2009, 1:43:04 PM7/6/09
to
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.

Uri Guttman

unread,
Jul 6, 2009, 2:01:04 PM7/6/09
to
>>>>> "r" == rabbits77 <rabb...@my-deja.com> 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!

RedGrittyBrick

unread,
Jul 7, 2009, 5:30:50 AM7/7/09
to

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

rabbits77

unread,
Jul 7, 2009, 1:50:06 PM7/7/09
to

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;

rabbits77

unread,
Jul 7, 2009, 2:07:37 PM7/7/09
to
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

Peter Makholm

unread,
Jul 7, 2009, 2:37:34 PM7/7/09
to
rabbits77 <rabb...@my-deja.com> writes:

> 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

Ben Morrow

unread,
Jul 7, 2009, 3:25:42 PM7/7/09
to

Quoth Peter Makholm <pe...@makholm.net>:

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

Ben

Uri Guttman

unread,
Jul 7, 2009, 3:41:17 PM7/7/09
to
>>>>> "BM" == Ben Morrow <b...@morrow.me.uk> writes:

BM> Quoth Peter Makholm <pe...@makholm.net>:


>> rabbits77 <rabb...@my-deja.com> writes:
>>
>> > 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?

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?

Tad J McClellan

unread,
Jul 7, 2009, 7:37:26 PM7/7/09
to
Uri Guttman <u...@stemsystems.com> wrote:
>>>>>> "BM" == Ben Morrow <b...@morrow.me.uk> writes:
>
> BM> Quoth Peter Makholm <pe...@makholm.net>:
> >> rabbits77 <rabb...@my-deja.com> writes:
> >>
> >> > 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?
>
> 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?


perl 4 is a wonderful language!

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

You know nothing!

I am all-knowing!

And I am a professor too!

So there.


PurlBoy

Charlton Wilbur

unread,
Jul 8, 2009, 12:42:13 PM7/8/09
to
>>>>> "TJM" == Tad J McClellan <ta...@seesig.invalid> writes:

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

--
Charlton Wilbur
cwi...@chromatico.net

0 new messages