如何在 Perl 中打印变量 [英] How to print variables in Perl
问题描述
我有一些代码看起来像
my ($ids,$nIds);
while (<myFile>){
chomp;
$ids.= $_ . " ";
$nIds++;
}
这应该连接我的 myFile
中的每一行,并且 nIds
应该是我的行数.如何打印出我的 $ids
和 $nIds
?
This should concatenate every line in my myFile
, and nIds
should be my number of lines. How do I print out my $ids
and $nIds
?
我尝试简单地print $ids
,但 Perl 抱怨.
I tried simply print $ids
, but Perl complains.
my ($ids, $nIds)
是一个列表,对吧?有两个元素?
is a list, right? With two elements?
推荐答案
print "Number of lines: $nids\n";
print "Content: $ids\n";
Perl 如何抱怨?print $ids
应该可以工作,尽管您可能希望在末尾有一个换行符,或者使用 print
显式地使用上述方法或隐式地使用 say
或 -l/$\.
How did Perl complain? print $ids
should work, though you probably want a newline at the end, either explicitly with print
as above or implicitly by using say
or -l/$\.
如果您想在字符串中插入一个变量并在其后紧接一些看起来像变量的一部分但实际上不是的内容,请将变量名称括在 {}
中:
If you want to interpolate a variable in a string and have something immediately after it that would looks like part of the variable but isn't, enclose the variable name in {}
:
print "foo${ids}bar";
这篇关于如何在 Perl 中打印变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!