FAQ
I thought I had done this before, but I guess not. I build a formt line for printf like:
q[%-3s%-4s%5s%6s];
But I want to insert a carriage return after say %-4s( I have a nubmer of fields and depending on the size, it is not a constant after column 2, but could be column 3 or 23.

I have tried:
q[%-3s%-4s] . qq[\\n] . q[%5s%6s]
or
q[%-3s%-4s\n%5s%6s]

But no matter what I do, it does not generate the carriage return. Shows up in the line. I know I am missing some very basic point, but has escaped me.

Here is the sample script I was playing with:
#!/usr/bin/perl

use strict;
use warnings;

my $MyLine1 = q[%2d %5s %6s];
my $MyLine2 = q[%2d %5s \n%6s];

printf "${MyLine1}\n",
2,
q[abc],
q[def];

printf "$MyLine2\n",
2,
q[abc],
q[def];

Thanks.

Wags ;)
David R. Wagner
Senior Programmer Analyst
FedEx Freight Systems
1.719.484.2097 Tel
1.719.484.2419 Fax
1.408.623.5963 Cell
http://fedex.com/us

Search Discussions

  • Shawn H Corey at Oct 23, 2009 at 1:19 pm

    Wagner, David --- Senior Programmer Analyst --- CFS wrote:
    I thought I had done this before, but I guess not. I build a formt line for printf like:
    q[%-3s%-4s%5s%6s];
    But I want to insert a carriage return after say %-4s( I have a nubmer of fields and depending on the size, it is not a constant after column 2, but could be column 3 or 23.

    I have tried:
    q[%-3s%-4s] . qq[\\n] . q[%5s%6s]
    q[%-3s%-4s] . qq[\n] . q[%5s%6s]

    Also:

    qq[\%-3s\%-4s\n\%5s\%6s];


    --
    Just my 0.00000002 million dollars worth,
    Shawn

    Programming is as much about organization and communication
    as it is about coding.

    I like Perl; it's the only language where you can bless your
    thingy.
  • Thomas Bätzler at Oct 26, 2009 at 7:49 am
    Hi,

    Wagner, David --- Senior Programmer Analyst --- CFS wrote:
    Here is the sample script I was playing with:
    #!/usr/bin/perl

    use strict;
    use warnings;

    my $MyLine1 = q[%2d %5s %6s];
    my $MyLine2 = q[%2d %5s \n%6s];
    The q// operator is equivalent to single quotes, so escape sequences like \n are not interpolated.

    If you want \n to be expanded to a newline character, you need to use qq or double quotes, or a combination of q// and qq// or quotes and text concatenation.

    HTH,
    Thomas

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedOct 22, '09 at 3:38p
activeOct 26, '09 at 7:49a
posts3
users3
websiteperl.org

People

Translate

site design / logo © 2021 Grokbase