Perl 中 $dummy 和非参数 split 意味着什么?

发布于 2024-12-21 12:56:54 字数 625 浏览 0 评论 0原文

我需要一些帮助来解码这个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

帅的被狗咬 2024-12-28 12:56:54
($dummy, $class) = split;

Split 返回一个值数组。第一个值放入 $dummy 中,第二个值放入 $class 中,任何其他值都将被忽略。第一个参数可能被命名为 dummy,因为作者计划忽略该值。更好的选择是使用 undef
忽略返回的条目: ( undef, $class ) = split;

Perldoc 可以向您展示 split 的功能。当不带参数调用时, split 将针对 $_ 进行操作并按空格进行拆分。 $_ 是 perl 中的默认变量,将其视为隐含的“it”,由上下文定义。

使用隐含的 $_ 可以使短代码更加简洁,但在较大的块中使用它的形式很糟糕。您不希望读者对您想要使用哪个“它”感到困惑。

split ;                      # split it
for (@list) { foo($_) }      # look at each element of list, foo it.
@new = map { $_ + 2 } @list ;# look at each element of list, 
                             # add 2 to it, put it in new list
while(<>){ foo($_)}          # grab each line of input, foo it.

perldoc -f 分割

如果省略 EXPR,则分割 $_ 字符串。如果 PATTERN 也被省略,则分割
空格(跳过任何前导空格后)。任何与模式匹配的东西
被视为分隔字段的分隔符。 (请注意,分隔符可以
长度超过一个字符。)

我是三元运算符 的忠实粉丝? : 用于设置字符串值并将逻辑推送到块和子例程中。

my $Truthsity = 9999
my $Falsity   = 2134123

print test_truthsity( $Truthsity, $Falsity, $_ );

sub test_truthsity {
  my ($truthsity, $falsity, $line ) = @_;
  my $newClass = $truthsity > $falsity ? 'truth' : 'lie';
  my (undef, $class) = split /\s+/, $line ;

  my $output = $class eq $newClass ? 'correct' 
             : $class eq 'true'    ? 'false neg'
             :                       'false pos';
  return $output;
}

此版本中可能存在一个微妙的错误。不带参数的 splitsplit(/\s+/, $_) 并不完全相同,如果行以空格开头,它们的行为会有所不同。在完全限定的分割中,返回空白前导字段。不带参数的 split 会删除前导空格。

$_ = "  ab cd";
my @a = split             # @a contains ( 'ab', 'cd' );
my @b = split /\s+/, $_;  # @b contains ( '', 'ab', 'cd')
($dummy, $class) = 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 to
ignore 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.

split ;                      # split it
for (@list) { foo($_) }      # look at each element of list, foo it.
@new = map { $_ + 2 } @list ;# look at each element of list, 
                             # add 2 to it, put it in new list
while(<>){ foo($_)}          # grab each line of input, foo it.

perldoc -f split

If EXPR is omitted, splits the $_ string. If PATTERN is also omitted, splits on
whitespace (after skipping any leading whitespace). Anything matching PATTERN
is taken to be a delimiter separating the fields. (Note that the delimiter may
be longer than one character.)

I'm a big fan of the ternary operator ? : for setting string values and of pushing logic into blocks and subroutines.

my $Truthsity = 9999
my $Falsity   = 2134123

print test_truthsity( $Truthsity, $Falsity, $_ );

sub test_truthsity {
  my ($truthsity, $falsity, $line ) = @_;
  my $newClass = $truthsity > $falsity ? 'truth' : 'lie';
  my (undef, $class) = split /\s+/, $line ;

  my $output = $class eq $newClass ? 'correct' 
             : $class eq 'true'    ? 'false neg'
             :                       'false pos';
  return $output;
}

There may be a subtle bug in this version. split with no args is not the exactly the same as split(/\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.

$_ = "  ab cd";
my @a = split             # @a contains ( 'ab', 'cd' );
my @b = split /\s+/, $_;  # @b contains ( '', 'ab', 'cd')
北斗星光 2024-12-28 12:56:54

来自 split 的文档:

分割/PATTERN/,EXPR

如果省略 EXPR,则分割 $_ 字符串。如果 PATTERN 也被省略,
在空格上分割(跳过任何前导空格后)。任何事物
匹配的 PATTERN 被视为分隔字段的分隔符。
(请注意,分隔符可能长于一个字符。)

因此,由于模式和表达式都被省略,我们将在空格上拆分默认变量 $_

$dummy 变量的目的是捕获从 split 返回的列表的第一个元素并忽略它,因为代码只对第二个元素感兴趣,该元素被放入 $class

您必须查看周围的代码才能找出此上下文中的 $_ 是什么;它可能是循环变量或 map 块中的列表项,或其他东西。

From the documentation for split:

split /PATTERN/,EXPR

If EXPR is omitted, splits the $_ string. If PATTERN is also omitted,
splits on whitespace (after skipping any leading whitespace). Anything
matching PATTERN is taken to be a delimiter separating the fields.
(Note that the delimiter may be longer than one character.)

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 a map block, or something else.

失与倦" 2024-12-28 12:56:54

如果您阅读文档,您会发现:

  • 第一个操作数的默认值是<代码>“”。
  • 第二个操作数的默认值是$_
  • 第三个操作数的默认值是0

so

split

是缩写

split " ", $_, 0

,意思是:

采用 $_,将其值拆分为空格,忽略前导和尾随空格。

第一个结果字段放置在 $dummy 中,第二个结果字段放置在 $class 中。

根据它的名称,我认为您永远不会再使用 $dummy,因此它只是充当占位符。不过,你可以摆脱它。

my ($dummy, $class) = split;

可以写成

my (undef, $class) = split;   # Use undef as a placeholder

my $class = ( split )[1];     # Use a list slice to get second item

If you read the documentation, you'll find that:

  • The default for the first operand is " ".
  • The default for the second operand is $_.
  • The default for the third operand is 0.

so

split

is short for

split " ", $_, 0

and it means:

Take $_, split its value on whitespace, ignoring leading and trailing whitespace.

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.

my ($dummy, $class) = split;

can be written as

my (undef, $class) = split;   # Use undef as a placeholder

or

my $class = ( split )[1];     # Use a list slice to get second item
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文