如何在 Perl printf 表达式中包含变量?

发布于 2024-08-22 03:23:26 字数 205 浏览 2 评论 0原文

如何在 printf 表达式中包含变量?

这是我的示例:

printf "%${cols}s", $_;

其中 $cols 是列数 $_ 是一个字符串。

该语句会导致“无效转换”警告。

问题最终是我忘记了删除变量。嘎。谢谢大家。

How can I include a variable in a printf expression?

Here's my example:

printf "%${cols}s", $_;

Where $cols is the number of columns
and $_ is a string.

The statement results in an "Invalid conversion" warning.

The problem ended up being that I forgot to chomp the variable. Gah. Thanks everyone.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

诠释孤独 2024-08-29 03:23:27

我无法重现你的问题。以下代码工作正常:

use strict;
use warnings;

my $cols=40;
while (<>) {
    printf "%${cols}s\n", $_;
}

它使用至少 40 列宽度打印任何输入行。

I cannot reproduce your problem. The following code works fine:

use strict;
use warnings;

my $cols=40;
while (<>) {
    printf "%${cols}s\n", $_;
}

It prints any input line using at least 40 columns of width.

戏蝶舞 2024-08-29 03:23:26

您的插值变量 $cols 看起来应该是一个数字,比如 10,所以

"%${cols}s"

应该插值并相当于

"%10s"

有效的格式字符串。

但是,如果 $cols 不是数字或有效格式字符串,您会收到警告。

例如,如果:

$cols = "w";

这将导致 "%ws" 作为格式字符串 - 给出您引用的错误:

Invalid conversion in printf: "%w"

可以找到有效的格式信息此处

Your interpolated variable $cols looks like its supposed to be a number, say 10, so

"%${cols}s"

should interpolate and be equivalent to

"%10s"

which is a valid format string.

If however $cols was something other than a number or valid format string, you'd get the warning.

For example, if:

$cols = "w";

that would result in "%ws" as a format string - giving the error you quote:

Invalid conversion in printf: "%w"

Valid format information can be found here.

嘿哥们儿 2024-08-29 03:23:26

我弄清楚了你的具体问题。你的代码是正确的。但是,我认为 $cols 可能是从用户输入中读取的数字,如下所示:

my $cols = <STDIN>;

这有效,并且在数字上下文中 $cols 将显示为一个数字,但是问题是 $cols 没有出现在此处的数字上下文中。它位于字符串上下文中,这意味着您的格式字符串不会扩展为 "%5s",而是扩展为 "%5\ns"。那里的换行符弄乱了格式字符串。

将您读取 $cols 的代码更改为:

chomp(my $cols = <STDIN>);

请参阅 < 上的文档code>chomp,因为您可能也想将它用于其他输入读取。

I figured out your specific problem. Your code is correct. However, I suppose $cols might be a number read from user input, say like this:

my $cols = <STDIN>;

This works, and in numeric context $cols will appear to be a number, but the problem is that $cols isn't appearing in numeric context here. It's in string context, which means that instead of expanding to "%5s", your format string expands to "%5\ns". The newline there is mucking up the format string.

Change the code where you read $cols to this:

chomp(my $cols = <STDIN>);

See the documentation on chomp, as you may want to use it for other input reading as well.

等往事风中吹 2024-08-29 03:23:26

始终在格式说明符中使用 * 来明确指示可变宽度!这类似于使用 printf "%s", $str 而不是 printf $str 的建议。

来自 关于 sprintf 的 perlfunc 文档:

(最小)宽度

参数通常被格式化为仅显示给定值所需的宽度。您可以通过在此处输入数字来覆盖宽度,或者从下一个参数(使用 *)或指定参数(例如 *2$)获取宽度:

printf '<%s>', "a"; # 打印“”
printf '<%6s>', "a"; # 打印“”
printf '<%*s>', 6, "a"; # 打印“”
printf '<%*2$s>', "a", 6; # 打印“”
printf '<%2s>', "长"; # 打印“<长>” (不截断)

如果通过*获得的字段宽度为负数,则与-标志具有相同的效果:左对齐。

例如:

#! /usr/bin/perl

use warnings;
use strict;

my $cols = 10;
$_ = "foo!";
printf "%*s\n", $cols, $_;
print "0123456789\n";

输出:

      foo!
0123456789

启用 warnings 编译指示后,您将看到非数字宽度参数的警告。

Always use * in your format specifier to unambiguously indicate variable width! This is similar to the advice to use printf "%s", $str rather than printf $str.

From the perlfunc documentation on sprintf:

(minimum) width

Arguments are usually formatted to be only as wide as required to display the given value. You can override the width by putting a number here, or get the width from the next argument (with *) or from a specified argument (with e.g. *2$):

printf '<%s>', "a";       # prints "<a>"
printf '<%6s>', "a";      # prints "<     a>"
printf '<%*s>', 6, "a";   # prints "<     a>"
printf '<%*2$s>', "a", 6; # prints "<     a>"
printf '<%2s>', "long";   # prints "<long>" (does not truncate)

If a field width obtained through * is negative, it has the same effect as the - flag: left-justification.

For example:

#! /usr/bin/perl

use warnings;
use strict;

my $cols = 10;
$_ = "foo!";
printf "%*s\n", $cols, $_;
print "0123456789\n";

Output:

      foo!
0123456789

With the warnings pragma enabled, you'll see warnings for non-numeric width arguments.

时光是把杀猪刀 2024-08-29 03:23:26

您当前的方法应该有效

perl -e'my $cols=500; $_="foo"; printf "%${cols}s\n\n", $_;'

Your current method should work

perl -e'my $cols=500; $_="foo"; printf "%${cols}s\n\n", $_;'
十年不长 2024-08-29 03:23:26

以下似乎对我有用

#!/bin/perl5.8 -w
use strict;
my $cols = 5;
my $a = "3";
printf "%${cols}d\n", $a;

28$ ./test.pl
    3

29$ 

The following seems to work for me:

#!/bin/perl5.8 -w
use strict;
my $cols = 5;
my $a = "3";
printf "%${cols}d\n", $a;

yields

28$ ./test.pl
    3

29$ 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文