gpt4 book ai didi

perl - foreach 语句中的值是如何打印的?

转载 作者:行者123 更新时间:2023-12-04 23:39:42 30 4
gpt4 key购买 nike

我对 perl 编程非常陌生。

在阅读循环时,对于 foreach 循环,我得到了两个示例。

一个例子是,

foreach ('hickory','dickory','doc') {
print $_;
print "\n";
}

输出:-
hickory
dickory
doc

$_ 变量包含每个项目。所以,它打印。

在另一个例子中,他们说没有在打印语句中指定 $_ 变量。空的打印语句只在那里。它如何打印 foreach 参数。
foreach ('hickory','dickory','doc') {
print;
print "\n";
}

输出:-
hickory
dickory
doc

对于这也是相同的输出。它如何打印值。在那本书中,他们没有对此作出任何解释。我在互联网上被搜索过。但我找不到任何东西。

最佳答案

您关于 print 的问题在 foreach正在回答,这里有更多关于 $_ 的信息.

来自 General Variables in perlvar

Here are the places where Perl will assume $_ even if you don't use it:

  • The following functions use $_ as a default argument:

    • abs, alarm, chomp, chop, chr, chroot, cos, defined, eval, evalbytes, exp, fc, glob, hex, int, lc, lcfirst, length, log, lstat, mkdir, oct, ord, pos, print, printf, quotemeta, readlink, readpipe, ref, require, reverse (in scalar context only), rmdir, say, sin, split (for its second argument), sqrt, stat, study, uc, ucfirst, unlink, unpack.
  • All file tests (-f , -d ) except for -t , which defaults to STDIN. See -X

  • The pattern matching operations m//, s/// and tr/// (aka y///) when used without an =~ operator.

  • The default iterator variable in a foreach loop if no other variable is supplied.

  • The implicit iterator variable in the grep() and map() functions.

  • The implicit variable of given().

  • The default place to put the next value or input record when a <FH>, readline, readdir or each operation's result is tested by itself as the sole criterion of a while test. Outside a while test, this will not happen.

  • $_ is by default a global variable.



如您所见,它几乎随处可用,并且确实被大量使用。请注意 perlvar页面描述了更多类似的变量,其中许多值得了解。

这是一个例子。考虑到我们从文件中读取行,想要丢弃只有空格或以 # 开头的行。 (评论),而对于其他人想用空格将它们分成单词。
open my $fh, '<', $file  or die "Can't open $file: $!";

while (<$fh>)
{
next if not /\S/;
next if /^\s*#/;

my @words = split;

# do something with @words ...
}

让我们看看 $_有多少用途在上面的例子中。这是一个等效的程序
while (my $line = <$fh>) 
{
next if not $line =~ m/\S/; # if not matching any non-space character
next if $line =~ m/^\s*#/; # if matching # after only (possible) spaces

my @words = split ' ', $line; # split $line by ' ' (any white space)

# do something with @words ...
}

比较这两个
  • 文件句柄读取 <$fh>while条件分配给 $_ ,然后在循环中可用。
  • 正则表达式的匹配运算符默认适用于 $_ . m本身可以被丢弃。
  • split默认拆分 $_ .我们还使用另一个默认值,作为分割字符串的模式,即 ' ' (任何数量的任何空白)。
  • 一旦我们这样做 $line = <$fh>$_ 的交易关闭(在循环中未定义),我们必须使用 $line到处。所以要么这样做,要么做 while (<$fh>)并使用 $_ .

  • 为了进一步说明这一切,让我们找出每一行中最长的大写单词
    use List::Util 'max';

    my $longest_cap = max map { length } grep { /^[A-Z]/ } @words;

    grep获取 @words 中的列表并将块应用于每个元素。每个元素都分配给 $_并且因此可用于块内的代码作为 $_ .这是正则表达式默认使用的。满足条件的传递给 map ,它也迭代将它们分配给 $_ ,当然是 length 的默认值.最后 max来自 List::Util选择最大的一个。

    请注意 $_从未真正写入,也不需要临时变量。

    这是一些相关的文档。 I/O Operators in perlop讨论 while (<$fh>)以及各种相关的事情。正则表达式部分在 Regexp Quote-Like Operators in perlop并在 perlretut .也看看 split .

    默认值经常使用,要阅读其他人编写的代码,您必须理解它们。当您编写自己的代码时,您可以选择是否使用 $_或者不,因为人们总是可以引入一个词法变量而不是它。

    那么,何时使用 $_作为默认值(不需要写),什么时候不写?

    正确使用默认值, $_特别是,可以使代码更清晰、更易读。通常意味着更好的代码。但是很可能把它推得太远,最终得到晦涩、棘手和脆弱的代码。所以需要好味道。

    另一种情况是当代码的某些部分受益于 $_对于它们的默认值,而在其他地方则必须使用 $_明确地。我会说如果 $_在一段代码中出现不止一次或两次,这意味着应该有一个正确命名的变量。

    总的来说,如果有疑问,只需命名一切。

    关于perl - foreach 语句中的值是如何打印的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41778211/

    30 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com