如何在 Perl 中打印变量 [英] How to print variables in Perl

查看:466
本文介绍了如何在 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆