Perl 过滤器重复说明

Perl 过滤器重复说明,perl,Perl,我是perl初学者。我尝试过滤重复条目。我在网上发现了一些神奇的东西,但我不太明白。有人能给我详细解释一下吗 my %seen; grep !$seen{$_}++, @_; %seen是一个散列变量。 @_是一个数组,它保存所有输入参数 循环第一次看到元素时,该元素在%SEED中没有键。下一次循环看到相同的元素时,它的键存在于散列中,并且该键的值为true,那么它将跳过该元素并转到下一个元素 您可以找到更多详细信息: 和%seen是一个散列变量。 @_是一个数组,它保存所有输入参数 循环第

我是perl初学者。我尝试过滤重复条目。我在网上发现了一些神奇的东西,但我不太明白。有人能给我详细解释一下吗

my %seen;
grep !$seen{$_}++, @_; 
%seen是一个散列变量。 @_是一个数组,它保存所有输入参数

循环第一次看到元素时,该元素在%SEED中没有键。下一次循环看到相同的元素时,它的键存在于散列中,并且该键的值为true,那么它将跳过该元素并转到下一个元素

您可以找到更多详细信息: 和

%seen是一个散列变量。 @_是一个数组,它保存所有输入参数

循环第一次看到元素时,该元素在%SEED中没有键。下一次循环看到相同的元素时,它的键存在于散列中,并且该键的值为true,那么它将跳过该元素并转到下一个元素

您可以找到更多详细信息:
而且

更详细地写出来可能会有所帮助

# Hash to keep track of what you've seen
my %seen;
# Array to store the first occurrence of each value
@values;

foreach my $x (@_) {
  # If we haven't seen this value already
  if (!$seen{$x}) {
    # Push this value onto @values
    push @values, $x;
  }

  # Increment the value in %seen to say we've seen this value
  $seen{$x}++;
}

# At the end, the unique values are in @values

更详细地写出来可能会有所帮助

# Hash to keep track of what you've seen
my %seen;
# Array to store the first occurrence of each value
@values;

foreach my $x (@_) {
  # If we haven't seen this value already
  if (!$seen{$x}) {
    # Push this value onto @values
    push @values, $x;
  }

  # Increment the value in %seen to say we've seen this value
  $seen{$x}++;
}

# At the end, the unique values are in @values