format and newline

Discussion in 'Perl Misc' started by cmic, Aug 11, 2006.

  1. cmic

    cmic Guest

    Hello.
    I have the following snippet :

    #!/usr/local/bin/perl
    use strict;
    use warnings;
    my @array=(2, 3, 5, 1, 5);;
    my ($a, $b, $c);
    format STDOUT =
    @>>>>>>>>|@<<<|@<<<<<
    $b, $a, $c
    ..
    foreach (@array) {
    $a=$_; $b=$a+100;$c=$a*2;
    write ;
    print "VAlue " . $a . "\n";
    }

    .... which prints :
    102|2 |4
    VAlue 2
    103|3 |6
    VAlue 3

    but and I want my script to print this instead :
    102|2 |4 VAlue 2
    103|3 |6 VAlue 3

    How to do this ? Or must I use sprintf instead ?
    TYA
    --
    cmic
     
    cmic, Aug 11, 2006
    #1
    1. Advertising

  2. cmic a écrit :
    > Hello.
    > I have the following snippet :
    >
    > #!/usr/local/bin/perl
    > use strict;
    > use warnings;
    > my @array=(2, 3, 5, 1, 5);;
    > my ($a, $b, $c);
    > format STDOUT =
    > @>>>>>>>>|@<<<|@<<<<<
    > $b, $a, $c
    > .
    > foreach (@array) {
    > $a=$_; $b=$a+100;$c=$a*2;
    > write ;
    > print "VAlue " . $a . "\n";
    > }
    >
    > ... which prints :
    > 102|2 |4
    > VAlue 2
    > 103|3 |6
    > VAlue 3
    >
    > but and I want my script to print this instead :
    > 102|2 |4 VAlue 2
    > 103|3 |6 VAlue 3
    >
    > How to do this ? Or must I use sprintf instead ?
    > TYA
    > --
    > cmic
    >


    Hi,

    You can use sprintf on $c value (be careful on the length) or create
    another column like this

    #!/usr/local/bin/perl
    use strict;
    use warnings;
    my @array=(2, 3, 5, 1, 5);;
    my ($a, $b, $c);
    format STDOUT =
    @>>>>>>>>|@<<<|@<<<<< @<<<<<<<<<
    $b, $a, $c, "VAlue $a"
    ..
    foreach (@array) {
    $a=$_; $b=$a+100;$c=$a*2;
    write ;
    }

    Hope this helps.

    Sebastien
     
    Sébastien Cottalorda, Aug 11, 2006
    #2
    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. =?Utf-8?B?QXNoYQ==?=

    string.split and Environment.NewLine question.

    =?Utf-8?B?QXNoYQ==?=, Dec 30, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    5,434
    Dan Bass
    Dec 30, 2004
  2. Mike Mimic

    fgets and newline

    Mike Mimic, May 15, 2004, in forum: C++
    Replies:
    4
    Views:
    8,018
    John Harrison
    May 15, 2004
  3. Samuel Shulman

    Labels and NewLine

    Samuel Shulman, Nov 27, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    345
    Samuel Shulman
    Nov 27, 2006
  4. versus
    Replies:
    6
    Views:
    10,430
  5. Kevin Walzer
    Replies:
    2
    Views:
    280
    Kevin Walzer
    Dec 2, 2012
Loading...

Share This Page