PERL for循环中$ _的下一个元素是什么

Sau*_*ava 0 perl

我想比较2个连续的循环元素

@arr=qw(1 2 3 3 4);
foreach(@arr)
{
if($_ == $_+1)
  {
  print "yes";
  }
}
Run Code Online (Sandbox Code Playgroud)

在这种情况下,我应该使用什么而不是$ _ + 1

Сух*_*й27 5

你必须循环索引,

my @arr = qw(1 2 3 3 4);
foreach (0 .. $#arr-1) {
  my ($current, $next) = @arr[$_, $_+1];

  if ($current == $next) {
    print "yes";
  }
}
Run Code Online (Sandbox Code Playgroud)