Format question

Discussion in 'Perl Misc' started by perlUSER, Sep 15, 2005.

  1. perlUSER

    perlUSER Guest

    Hello;
    I started learning Perl recently and am stuck with the Perl "format"
    function.
    ----------------------------------------------------------------
    Program:
    #!/usr/bin/perl
    use lib '/tmp/perl';
    use FileHandle;

    format STDOUT =
    Album=@<<<<<<<<<<<<< Artist=@>>>>>>>>>>>> Price=$@##.##
    $album, $artist, $price
    ..

    $album = "Test album";
    $artist = "Brit";
    $price = "9.99";
    write();
    ================================================================
    Output:
    $ perl rpt.pl

    Album= Artist= Price=$ 0.00
    $album, $artist, $price

    $
    ===============================================================
    Variables are printed as if they are like static values.

    Could you please help me with this?

    Thanks in advance.

    Regards,
    Sri.
     
    perlUSER, Sep 15, 2005
    #1
    1. Advertising

  2. perlUSER

    John Bokma Guest

    "perlUSER" <> wrote:

    > Hello;
    > I started learning Perl recently and am stuck with the Perl "format"
    > function.
    > ----------------------------------------------------------------
    > Program:
    > #!/usr/bin/perl
    > use lib '/tmp/perl';
    > use FileHandle;
    >
    > format STDOUT =
    > Album=@<<<<<<<<<<<<< Artist=@>>>>>>>>>>>> Price=$@##.##
    > $album, $artist, $price
    > .
    >
    > $album = "Test album";
    > $artist = "Brit";
    > $price = "9.99";
    > write();
    > ================================================================
    > Output:
    > $ perl rpt.pl
    >
    > Album= Artist= Price=$ 0.00
    > $album, $artist, $price
    >
    > $
    > ===============================================================
    > Variables are printed as if they are like static values.


    perl form.pl
    Album=Test album Artist= Brit Price=$ 9.99

    ActiveState Perl @ XP.

    --
    John Small Perl scripts: http://johnbokma.com/perl/
    Perl programmer available: http://castleamber.com/
    Happy Customers: http://castleamber.com/testimonials.html
     
    John Bokma, Sep 15, 2005
    #2
    1. Advertising

  3. perlUSER

    perlUSER Guest

    John,
    Could you please let me know what I am missing?

    Sri.
     
    perlUSER, Sep 15, 2005
    #3
  4. perlUSER

    perlUSER Guest

    Jim,
    Thank you for sharing your thoughts. What is the equivalent function
    for format or what do you recommend to a learner.

    Regards,
    Srini.
     
    perlUSER, Sep 15, 2005
    #4
  5. perlUSER

    John Bokma Guest

    "perlUSER" <> wrote:

    > John,
    > Could you please let me know what I am missing?


    I really don't know. I haven't used formats for ages, just copied your
    example, and it works here. I recommend however to add:

    use strict;
    use warnings;

    There are people who don't look at your code without those two, often for
    very good reasons.

    As Jim already suggested, have a look at perldoc perlform.

    --
    John Small Perl scripts: http://johnbokma.com/perl/
    Perl programmer available: http://castleamber.com/
    Happy Customers: http://castleamber.com/testimonials.html
     
    John Bokma, Sep 15, 2005
    #5
  6. perlUSER

    Paul Lalli Guest

    [Please quote some context when you are replying. Thank you.]

    perlUSER wrote:

    > Thank you for sharing your thoughts. What is the equivalent function
    > for format or what do you recommend to a learner.


    I've never really understood what formats can do that a simple
    sprintf() cannot, personally.

    Using your example:

    #!/usr/bin/perl
    use strict;
    use warnings;

    my $album = "Test album";
    my $artist = "Brit";
    my $price = "9.99";
    printf "Album=%-13s Artist=%12s Price=\$%05.2f\n",
    $album, $artist, $price;

    __END__
    Album=Test album Artist= Brit Price=$09.99

    for more information,
    perldoc -f sprintf

    Hope this helps,
    Paul Lalli
     
    Paul Lalli, Sep 15, 2005
    #6
  7. perlUSER

    Guest

    Paul Lalli wrote:

    > > Thank you for sharing your thoughts. What is the equivalent function
    > > for format or what do you recommend to a learner.

    >
    > I've never really understood what formats can do that a simple
    > sprintf() cannot, personally.
    >


    I don't recall if sprintf() is as full featured, but formats did
    provide
    a good visual preview of how the output will be rendered on the page.
    Right/left justification and padding tokens were easy to remember and
    usually more intuitive than with sprintf.

    --
    Charles DeRykus
     
    , Sep 15, 2005
    #7
  8. perlUSER <> wrote:

    > format STDOUT =
    > Album=@<<<<<<<<<<<<< Artist=@>>>>>>>>>>>> Price=$@##.##
    > $album, $artist, $price
    > .


    > $ perl rpt.pl


    > Album= Artist= Price=$ 0.00
    > $album, $artist, $price


    There must be two separate lines under the 'format =' bit. If instead
    you have one long line that wraps around on the screen, you would get
    that output.

    Just cutting and pasting your script into my machine gave me the
    expected output. When I joined the two format lines together into one,
    I got your output instead.

    --
    Darren Dunham
    Senior Technical Consultant TAOS http://www.taos.com/
    Got some Dr Pepper? San Francisco, CA bay area
    < This line left intentionally blank to confuse you. >
     
    Darren Dunham, Sep 15, 2005
    #8
  9. <> wrote:

    > Paul Lalli wrote:


    >> > Thank you for sharing your thoughts. What is the equivalent function
    >> > for format or what do you recommend to a learner.

    >>
    >> I've never really understood what formats can do that a simple
    >> sprintf() cannot, personally.
    >>


    > I don't recall if sprintf() is as full featured, but formats did
    > provide
    > a good visual preview of how the output will be rendered on the page.
    > Right/left justification and padding tokens were easy to remember and
    > usually more intuitive than with sprintf.


    Also, it has some basic page support for standard page lengths. Most
    output formats probably don't stick to 66 lines per page any longer, but
    I used to use that to good effect... :)

    --
    Darren Dunham
    Senior Technical Consultant TAOS http://www.taos.com/
    Got some Dr Pepper? San Francisco, CA bay area
    < This line left intentionally blank to confuse you. >
     
    Darren Dunham, Sep 15, 2005
    #9
  10. perlUSER

    George Guest

    perlUSER wrote:

    > Hello;
    > I started learning Perl recently and am stuck with the Perl "format"
    > function.
    > ----------------------------------------------------------------
    > Program:
    > #!/usr/bin/perl
    > use lib '/tmp/perl';
    > use FileHandle;
    >
    > format STDOUT =
    > Album=@<<<<<<<<<<<<< Artist=@>>>>>>>>>>>> Price=$@##.##
    > $album, $artist, $price
    > .
    >
    > $album = "Test album";
    > $artist = "Brit";
    > $price = "9.99";
    > write();
    > ================================================================
    > Output:
    > $ perl rpt.pl
    >
    > Album= Artist= Price=$ 0.00
    > $album, $artist, $price
    >
    > $
    > ===============================================================
    > Variables are printed as if they are like static values.
    >
    > Could you please help me with this?
    >
    > Thanks in advance.
    >
    > Regards,
    > Sri.


    Though you code works like Bokma says,
    but any way try this

    use warnings;
    use strict;
    my ($album,$artist,$price);
    format PRINT =
    Album=@<<<<<<<<<<<<<, Artist=@<<<<<, Price=$@##.##
    $album, $artist, $price
    ..

    $album = "Test album";
    $artist = "Brit";
    $price = "9.99";
    $~="PRINT";
    write();

    --
     
    George, Sep 16, 2005
    #10
    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,194
    Janaka
    Feb 18, 2004
  2. Andy
    Replies:
    7
    Views:
    6,319
    Roedy Green
    May 10, 2004
  3. shannon
    Replies:
    1
    Views:
    8,632
  4. shannon
    Replies:
    0
    Views:
    588
    shannon
    Feb 1, 2006
  5. Replies:
    6
    Views:
    479
    Thomas Ganss
    Jan 15, 2006
Loading...

Share This Page