Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am getting a warning: Newline in left-justified string for %s

In the following sprintf:

join(" & ", map { sprintf "%-8s (x%s)", $_, 0+@{$rel_hosts{$_}} } @rel +);
I read perldoc perldiag, where the warning is explained:
(W printf) There is a newline in a string to be left justified by printf or sprintf. The padding spaces will appear after the newline, which is probably not what you wanted. Usually you should remove the newline from the string and put formatting characters in the sprintf format.
So following this advice, I decided that I needed to strip the newline(s) out and add them back in the %s. To help me understand the problem further, I tried to reproduce the error with the following code:
my @rel = ("test1", "test2", "test3"); my $test = join(" & ", map { sprintf "%-8s (x%s)", "$_\n"} @rel);
To my surprise this code did not generate the warning. Why not? And how can I make it do so?

And how would you fix the original problem?

Replies are listed 'Best First'.
Re: Newline in left-justified string for %s warning
by GrandFather (Saint) on Oct 10, 2005 at 04:21 UTC

    When I run your two line of code I get the following warnings:

    Newline in left-justified string for sprintf at ...noname.pl line 6. Use of uninitialized value in sprintf at ...noname.pl line 6. Newline in left-justified string for sprintf at ...noname.pl line 6. Use of uninitialized value in sprintf at ...noname.pl line 6. Newline in left-justified string for sprintf at ...noname.pl line 6. Use of uninitialized value in sprintf at ...noname.pl line 6.

    Have you omited use warnings?


    Perl is Huffman encoded by design.

      This code helps to explain those two warnings seperately:

      printf("%-2s", "\n"); printf("%s%s", "a");

      It prints:

      Newline in left-justified string for printf at math1.pl line 1. Use of uninitialized value in printf at math1.pl line 2. a

      The OP already provided the document that explains the first warning. The warning, to be frank, is meaningless. They just feel worried that you are probably confusing yourself ;-)

      The second warning was caused by the fact that we have two %s, which requires two strings, but only one is provided.

      I am using warnings and I am not getting the warnings. My Perl version:

      This is perl, v5.6.1 built for i686-linux

      The original sprintf that gives the warning is most likely running on a different platform (although I would have to check).

        I am using Active State v5.8.7 built for MSWin32-x86-multi-thread.


        Perl is Huffman encoded by design.
Re: Newline in left-justified string for %s warning
by GrandFather (Saint) on Oct 10, 2005 at 04:30 UTC

    What do you really want to generate? The Use of uninitialized value in sprintf is because you have two %ss in your format string, but only provide one string.

    Do you mean something like this:

    use warnings; use strict; my @rel = ("test1", "test2", "test3"); print join " &\n ", @rel;

    which prints:

    test1 & test2 & test3

    Perl is Huffman encoded by design.
Re: Newline in left-justified string for %s warning
by Skeeve (Parson) on Oct 10, 2005 at 05:37 UTC

    Why do you think, it helps, removing the newline and adding it again before applying sprintf? Why do you think, you need a newline at that position?

    Maybe this is what yo really wanted?

    s/[\015\012]//g foreach @rel; # first remove *all* newlines join(" & ", map { sprintf "%-8s (x%s)", $_, 0+@{$rel_hosts{$_}} } @rel +) . "\n"; # add it at the end

    $\=~s;s*.*;q^|D9JYJ^^qq^\//\\\///^;ex;print
Re: Newline in left-justified string for %s warning
by graff (Chancellor) on Oct 10, 2005 at 12:32 UTC
    As indicated by Skeeve, the problem hinges on how you are assigning values to your @rel array. In your latter example, where you just assign a set of quoted strings -- and none of those strings contain a newline ("\n") -- you don't get the warning, because the values you pass to sprintf to fill the "%-8s" left-justified field do not contain newlines. (And if you provide two values for the two "%s" slots, you won't get any warnings.)

    Meanwhile, if you were originally assigning values to @rel via input from STDIN or another file handle, then it would appear that the strings all have a "\n" at the end (or a literal "\r\n", depending on your OS).

    If you "chomp" @rel before passing the values to sprintf, there will be no warnings about newlines -- and the output will appear as specified in the format string, without a newline showing up in front of the " (x".

    For that matter, it would be best to do  chomp @rel before you use those values as hash keys in %rel_hosts. Having newlines at the ends of hash key values strings is quite unnecessary and generally undesirable.

Re: Newline in left-justified string for %s warning
by EvanCarroll (Chaplain) on Oct 10, 2005 at 04:23 UTC
    You beat me to the post, I get errors too.
    root@ANON:/var/www/auth_required # perl -we'my $test = join(" & ", map + { sprintf "%-8s (x%s)", "$_\n"} qw/foo bar baz/)' Newline in left-justified string for sprintf at -e line 1. Use of uninitialized value in sprintf at -e line 1. Newline in left-justified string for sprintf at -e line 1. Use of uninitialized value in sprintf at -e line 1. Newline in left-justified string for sprintf at -e line 1. Use of uninitialized value in sprintf at -e line 1.


    Evan Carroll
    www.EvanCarroll.com