Perl 中 $dummy 和非参数 split 意味着什么?
我需要一些帮助来解码这个 perl 脚本。 $dummy 未在脚本中的其他任何地方使用任何内容进行初始化。脚本中的以下行是什么意思?为什么 split 函数没有任何参数时会出现这种情况?
($dummy, $class) = split;
该程序试图使用某种统计分类方法来检查某个陈述是真还是假。假设它计算并给出以下数字“真值”和“假值”,然后检查测谎仪是否正确。
# some code, some code...
$_ = "truth"
# more some code, some code ...
$Truthsity = 9999
$Falsity = 2134123
if ($Truthsity > $Falsity) {
$newClass = "truth";
} else {
$newClass = "lie";
}
($dummy, $class) = split;
if ($class eq $newClass) {
print "correct";
} elsif ($class eq "true") {
print "false neg";
} else {
print "false pos"
}
I need some help decoding this perl script. $dummy is not initialized with anything throughout anywhere else in the script. What does the following line mean in the script? and why does it mean when the split function doesn't have any parameter?
($dummy, $class) = split;
The program is trying to check whether a statement is truth or lie using some statistical classification method. So lets say it calculates and give the following number to "truth-sity" and "falsity" then it checks whether the lie detector is correct or not.
# some code, some code...
$_ = "truth"
# more some code, some code ...
$Truthsity = 9999
$Falsity = 2134123
if ($Truthsity > $Falsity) {
$newClass = "truth";
} else {
$newClass = "lie";
}
($dummy, $class) = split;
if ($class eq $newClass) {
print "correct";
} elsif ($class eq "true") {
print "false neg";
} else {
print "false pos"
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Split 返回一个值数组。第一个值放入
$dummy
中,第二个值放入$class
中,任何其他值都将被忽略。第一个参数可能被命名为 dummy,因为作者计划忽略该值。更好的选择是使用 undef忽略返回的条目:
( undef, $class ) = split;
Perldoc 可以向您展示 split 的功能。当不带参数调用时, split 将针对
$_
进行操作并按空格进行拆分。$_
是 perl 中的默认变量,将其视为隐含的“it”,由上下文定义。使用隐含的 $_ 可以使短代码更加简洁,但在较大的块中使用它的形式很糟糕。您不希望读者对您想要使用哪个“它”感到困惑。
perldoc -f 分割
我是三元运算符
的忠实粉丝? :
用于设置字符串值并将逻辑推送到块和子例程中。此版本中可能存在一个微妙的错误。不带参数的
split
与split(/\s+/, $_)
并不完全相同,如果行以空格开头,它们的行为会有所不同。在完全限定的分割中,返回空白前导字段。不带参数的split
会删除前导空格。Split returns an array of values. The first is put into
$dummy
, the second into$class
, and any further values are ignored. The first arg is likely named dummy because the author plans to ignore that value. A better option is to use undef toignore a returned entry:
( undef, $class ) = split;
Perldoc can show you how split functions. When called without arguments, split will operate against
$_
and split on whitespace.$_
is the default variable in perl, think of it as an implied "it," as defined by context.Using an implied $_ can make short code more concise, but it's poor form to use it inside larger blocks. You don't want the reader to get confused about which 'it' you want to work with.
perldoc -f split
I'm a big fan of the ternary operator
? :
for setting string values and of pushing logic into blocks and subroutines.There may be a subtle bug in this version.
split
with no args is not the exactly the same assplit(/\s+/, $_)
, they behave differently if the line starts with spaces. In fully qualified split, blank leading fields are returned.split
with no args drops the leading spaces.来自
split
的文档:因此,由于模式和表达式都被省略,我们将在空格上拆分默认变量
$_
。$dummy
变量的目的是捕获从 split 返回的列表的第一个元素并忽略它,因为代码只对第二个元素感兴趣,该元素被放入$class
。您必须查看周围的代码才能找出此上下文中的
$_
是什么;它可能是循环变量或map
块中的列表项,或其他东西。From the documentation for
split
:So since both the pattern and the expression are omitted, we are splitting the default variable
$_
on whitespace.The purpose of the
$dummy
variable is to capture the first element of the list returned from split and ignore it, because the code is only interested in the second element, which gets put into$class
.You'll have to look at the surrounding code to find out what
$_
is in this context; it may be a loop variable or a list item in amap
block, or something else.如果您阅读文档,您会发现:
$_
。0
。so
是缩写
,意思是:
第一个结果字段放置在
$dummy
中,第二个结果字段放置在$class
中。根据它的名称,我认为您永远不会再使用
$dummy
,因此它只是充当占位符。不过,你可以摆脱它。可以写成
或
If you read the documentation, you'll find that:
" "
.$_
.0
.so
is short for
and it means:
The first resulting field is placed in
$dummy
, and the second in$class
.Based on its name, I presume you proceed to never use
$dummy
again, so it's simply acting as a placeholder. You can get rid of it, though.can be written as
or