[英]How to print variables in Perl
I have some code that looks like我有一些看起来像的代码
my ($ids,$nIds);
while (<myFile>){
chomp;
$ids.= $_ . " ";
$nIds++;
}
This should concatenate every line in my myFile
, and nIds
should be my number of lines.这应该连接
myFile
中的每一行,并且nIds
应该是我的行数。 How do I print out my $ids
and $nIds
?如何打印出我的
$ids
和$nIds
?
I tried simply print $ids
, but Perl complains.我尝试简单地
print $ids
,但 Perl 抱怨。
my ($ids, $nIds)
is a list, right?是一个列表,对吧? With two elements?
有两个元素?
print "Number of lines: $nids\n";
print "Content: $ids\n";
How did Perl complain? Perl 是如何抱怨的?
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 / $\ . print $ids
应该可以工作,尽管您可能希望在末尾有一个换行符,或者像上面那样显式地使用print
或使用say
或-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";
You should always include all relevant code when asking a question.在提出问题时,您应该始终包含所有相关代码。 In this case, the print statement that is the center of your question.
在这种情况下,打印语句是您问题的中心。 The print statement is probably the most crucial piece of information.
打印语句可能是最重要的信息。 The second most crucial piece of information is the error, which you also did not include.
第二个最重要的信息是错误,您也没有包括在内。 Next time, include both of those.
下一次,包括这两个。
print $ids
should be a fairly hard statement to mess up, but it is possible. print $ids
应该是一个相当难以搞砸的声明,但它是可能的。 Possible reasons:可能的原因:
$ids
is undefined. $ids
未定义。 Gives the warning undefined value in print
undefined value in print
$ids
is out of scope. $ids
超出 scope。 With use strict
, gives fatal warning Global variable $ids needs explicit package name
, and otherwise the undefined warning from above. use strict
,给出致命警告Global variable $ids needs explicit package name
,否则来自上面的未定义警告。print $ids $nIds
, in which case perl thinks that $ids
is supposed to be a filehandle, and you get an error such as print to unopened filehandle
.print $ids $nIds
,在这种情况下 perl 认为$ids
应该是一个文件句柄,并且您收到诸如print to unopened filehandle
unopened filehandle 之类的错误。 Explanations解释
1: Should not happen. 1:不应该发生。 It might happen if you do something like this (assuming you are not using
strict
):如果你做这样的事情可能会发生(假设你没有使用
strict
):
my $var;
while (<>) {
$Var .= $_;
}
print $var;
Gives the warning for undefined value, because $Var
and $var
are two different variables.给出未定义值的警告,因为
$Var
和$var
是两个不同的变量。
2: Might happen, if you do something like this: 2:可能会发生,如果你做这样的事情:
if ($something) {
my $var = "something happened!";
}
print $var;
my
declares the variable inside the current block. my
在当前块内声明变量。 Outside the block, it is out of scope.块外,是scope。
3: Simple enough, common mistake, easily fixed. 3:足够简单,常见错误,容易修复。 Easier to spot with
use warnings
. use warnings
更容易发现。
4: Also a common mistake. 4:也是一个常见的错误。 There are a number of ways to correctly print two variables in the same
print
statement:有多种方法可以在同一个
print
语句中正确打印两个变量:
print "$var1 $var2"; # concatenation inside a double quoted string
print $var1 . $var2; # concatenation
print $var1, $var2; # supplying print with a list of args
Lastly, some perl magic tips for you:最后,为您提供一些 perl 魔术提示:
use strict;
use warnings;
# open with explicit direction '<', check the return value
# to make sure open succeeded. Using a lexical filehandle.
open my $fh, '<', 'file.txt' or die $!;
# read the whole file into an array and
# chomp all the lines at once
chomp(my @file = <$fh>);
close $fh;
my $ids = join(' ', @file);
my $nIds = scalar @file;
print "Number of lines: $nIds\n";
print "Text:\n$ids\n";
Reading the whole file into an array is suitable for small files only, otherwise it uses a lot of memory.将整个文件读入数组只适合小文件,否则会用到很多memory。 Usually, line-by-line is preferred.
通常,首选逐行。
Variations:变化:
print "@file"
is equivalent to $ids = join(' ',@file); print $ids;
print "@file"
等价于$ids = join(' ',@file); print $ids;
$ids = join(' ',@file); print $ids;
$#file
will return the last index in @file
. $#file
将返回@file
中的最后一个索引。 Since arrays usually start at 0, $#file + 1
is equivalent to scalar @file
.$#file + 1
相当于scalar @file
。 You can also do:你也可以这样做:
my $ids;
do {
local $/;
$ids = <$fh>;
}
By temporarily "turning off" $/
, the input record separator, ie newline, you will make <$fh>
return the entire file.通过暂时“关闭”
$/
,输入记录分隔符,即换行符,您将使<$fh>
返回整个文件。 What <$fh>
really does is read until it finds $/
, then return that string. <$fh>
真正做的是读取直到找到$/
,然后返回该字符串。 Note that this will preserve the newlines in $ids
.请注意,这将保留
$ids
中的换行符。
Line-by-line solution:逐行解决方案:
open my $fh, '<', 'file.txt' or die $!; # btw, $! contains the most recent error
my $ids;
while (<$fh>) {
chomp;
$ids .= "$_ "; # concatenate with string
}
my $nIds = $.; # $. is Current line number for the last filehandle accessed.
How do I print out my $ids and $nIds?如何打印出我的 $ids 和 $nIds?
print "$ids\n";
print "$nIds\n";
I tried simplyprint $ids
, but Perl complains.我尝试简单地
print $ids
,但 Perl 抱怨。
Complains about what?抱怨什么? Uninitialised value?
未初始化的值? Perhaps your loop was never entered due to an error opening the file.
由于打开文件时出错,您的循环可能从未进入。 Be sure to check if
open
returned an error, and make sure you are using use strict; use warnings;
请务必检查
open
是否返回错误,并确保您使用的是use strict; use warnings;
use strict; use warnings;
. .
my ($ids, $nIds)
is a list, right?my ($ids, $nIds)
是一个列表,对吗? With two elements?有两个元素?
It's a (very special) function call.这是一个(非常特别的)function 调用。
$ids,$nIds
is a list with two elements. $ids,$nIds
是一个包含两个元素的列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.