Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
In the following sprintf:
I read perldoc perldiag, where the warning is explained:join(" & ", map { sprintf "%-8s (x%s)", $_, 0+@{$rel_hosts{$_}} } @rel +);
(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:
To my surprise this code did not generate the warning. Why not? And how can I make it do so?my @rel = ("test1", "test2", "test3"); my $test = join(" & ", map { sprintf "%-8s (x%s)", "$_\n"} @rel);
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 | |
by pg (Canon) on Oct 10, 2005 at 05:26 UTC | |
by Anonymous Monk on Oct 10, 2005 at 04:28 UTC | |
by GrandFather (Saint) on Oct 10, 2005 at 04:34 UTC | |
|
Re: Newline in left-justified string for %s warning
by GrandFather (Saint) on Oct 10, 2005 at 04:30 UTC | |
|
Re: Newline in left-justified string for %s warning
by Skeeve (Parson) on Oct 10, 2005 at 05:37 UTC | |
|
Re: Newline in left-justified string for %s warning
by graff (Chancellor) on Oct 10, 2005 at 12:32 UTC | |
|
Re: Newline in left-justified string for %s warning
by EvanCarroll (Chaplain) on Oct 10, 2005 at 04:23 UTC |