how to format output

Discussion in 'Perl Misc' started by moonhkt, Jul 6, 2009.

  1. moonhkt

    moonhkt Guest

    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");
    moonhkt, Jul 6, 2009
    #1
    1. Advertising

  2. moonhkt

    rabbits77 Guest

    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.
    rabbits77, Jul 6, 2009
    #2
    1. Advertising

  3. moonhkt

    Uri Guttman Guest

    >>>>> "r" == rabbits77 <> 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.sysarch.com --
    ----- Perl Code Review , Architecture, Development, Training, Support ------
    --------- Free Perl Training --- http://perlhunter.com/college.html ---------
    --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
    Uri Guttman, Jul 6, 2009
    #3
  4. moonhkt

    rabbits77 Guest

    Uri Guttman wrote:
    >>>>>> "r" == rabbits77 <> 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!
    rabbits77, Jul 6, 2009
    #4
  5. moonhkt

    Uri Guttman Guest

    >>>>> "r" == rabbits77 <> writes:

    r> Uri Guttman wrote:
    >>>>>>> "r" == rabbits77 <> 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 ------ -------- 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 ---------
    Uri Guttman, Jul 6, 2009
    #5
  6. moonhkt

    rabbits77 Guest

    Uri Guttman wrote:
    >>>>>> "r" == rabbits77 <> writes:

    >
    > r> Uri Guttman wrote:
    > >>>>>>> "r" == rabbits77 <> 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.
    rabbits77, Jul 6, 2009
    #6
  7. moonhkt

    Uri Guttman Guest

    >>>>> "r" == rabbits77 <> 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 ------ -------- 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 ---------
    Uri Guttman, Jul 6, 2009
    #7
  8. 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
    RedGrittyBrick, Jul 7, 2009
    #8
  9. moonhkt

    rabbits77 Guest

    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");
    rabbits77, Jul 7, 2009
    #9
  10. moonhkt

    rabbits77 Guest

    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
    rabbits77, Jul 7, 2009
    #10
  11. rabbits77 <> 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
    Peter Makholm, Jul 7, 2009
    #11
  12. moonhkt

    Uri Guttman Guest

    >>>>> "BM" == Ben Morrow <> writes:

    BM> Quoth Peter Makholm <>:
    >> rabbits77 <> 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?

    uri

    --
    Uri Guttman ------ -------- 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 ---------
    Uri Guttman, Jul 7, 2009
    #12
  13. >>>>> "TJM" == Tad J McClellan <> 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
    Charlton Wilbur, Jul 8, 2009
    #13
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian Candy
    Replies:
    2
    Views:
    1,159
    Janaka
    Feb 18, 2004
  2. Andy
    Replies:
    7
    Views:
    6,255
    Roedy Green
    May 10, 2004
  3. shannon
    Replies:
    1
    Views:
    8,595
  4. shannon
    Replies:
    0
    Views:
    569
    shannon
    Feb 1, 2006
  5. Replies:
    6
    Views:
    462
    Thomas Ganss
    Jan 15, 2006
Loading...

Share This Page