如何在Perl中打印变量

mon*_*ing 28 perl

我有一些看起来像的代码

my ($ids,$nIds);
while (<myFile>){
    chomp;
    $ids.= $_ . " ";
    $nIds++;
}
Run Code Online (Sandbox Code Playgroud)

这应该连接我的每一行myFile,并且nIds应该是我的行数.如何打印出我$ids$nIds

我试过简单print $ids,但Perl抱怨道.

my ($ids, $nIds)
Run Code Online (Sandbox Code Playgroud)

是一个清单,对吗?有两个元素?

yst*_*sth 34

print "Number of lines: $nids\n";
print "Content: $ids\n";
Run Code Online (Sandbox Code Playgroud)

Perl怎么抱怨? print $ids应该工作,虽然你可能想在最后一个换行符,或者明确地print如上或隐式使用say-l/$ \.

如果你想在一个字符串中插入一个变量并在它之后有一些看起来像是变量的一部分而不是变量的一部分,那么将变量名括在{}:

print "foo${ids}bar";
Run Code Online (Sandbox Code Playgroud)


TLP*_*TLP 12

在提问时,您应始终包含所有相关代码.在这种情况下,print语句是您问题的中心.print语句可能是最重要的信息.第二个最重要的信息是错误,您也没有包含这些错误.下一次,包括这两个.

print $ids应该是一个相当硬的陈述,但这是可能的.可能的原因:

  1. $ids未定义.发出警告undefined value in print
  2. $ids超出范围.随着use strict,发出致命警告Global variable $ids needs explicit package name,以及来自上方的未定义警告.
  3. 你忘记了行尾的一个分号.
  4. 你试图这样做print $ids $nIds,在这种情况下perl认为$ids 应该是一个文件句柄,你会得到一个错误,如print to unopened filehandle.

说明

1:不应该发生.如果您执行此类操作(假设您未使用),则可能会发生这种情况strict:

my $var;
while (<>) {
    $Var .= $_;
}
print $var;
Run Code Online (Sandbox Code Playgroud)

给出未定义值的警告,因为$Var$var是两个不同的变量.

2:如果你这样做,可能会发生:

if ($something) {
    my $var = "something happened!";
}
print $var;
Run Code Online (Sandbox Code Playgroud)

my声明当前块内的变量.在街区之外,它超出了范围.

3:足够简单,常见错误,容易修复.更容易发现use warnings.

4:也是一个常见的错误.有多种方法可以在同一print语句中正确打印两个变量:

print "$var1 $var2";  # concatenation inside a double quoted string
print $var1 . $var2;  # concatenation
print $var1, $var2;   # supplying print with a list of args
Run Code Online (Sandbox Code Playgroud)

最后,一些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";
Run Code Online (Sandbox Code Playgroud)

将整个文件读入数组仅适用于小文件,否则会占用大量内存.通常,逐行是优选的.

变化:

  • print "@file" 相当于 $ids = join(' ',@file); print $ids;
  • $#file将返回最后一个索引@file.由于数组通常从0开始, $#file + 1相当于scalar @file.

你也可以这样做:

my $ids;
do {
    local $/;
    $ids = <$fh>;
}
Run Code Online (Sandbox Code Playgroud)

通过暂时"关闭" $/,输入记录分隔符,即换行符,您将<$fh>返回整个文件.什么<$fh>确实被读取,直到它找到$/,然后返回该字符串.请注意,这将保留新行$ids.

逐行解决方案:

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.
Run Code Online (Sandbox Code Playgroud)


ike*_*ami 9

如何打印我的$ ID和$ nIds?
print "$ids\n";
print "$nIds\n";
Run Code Online (Sandbox Code Playgroud)
我试过简单print $ids,但Perl抱怨道.

抱怨什么?未初始化的价值?由于打开文件时出错,可能从未输入过循环.请务必检查是否open返回错误,并确保您正在使用use strict; use warnings;.

my ($ids, $nIds)是一个清单,对吗?有两个元素?

这是一个(非常特殊的)函数调用.$ids,$nIds是一个包含两个元素的列表.