Case Insensitive Grep?

use*_*502 0 perl grep

我在PERL中使用这个grep语句来查找非重复的名称.任何人都可以告诉我如何使它不区分大小写?我知道我需要使用'i',但我不知道它在哪里.谢谢!

@nondup = grep {$marked{$_}++; $marked{$_} = 1;} @names
Run Code Online (Sandbox Code Playgroud)

Mil*_*ler 5

使用fc(Perl 5.16或更新版本)或lc:

use strict;
use warnings;
use feature 'fc';

my @names = qw(apple Apple foo bar baz BaZ bar);

my %seen;
my @unique = grep {! $seen{fc $_}++} @names;

print "@unique";
Run Code Online (Sandbox Code Playgroud)

输出:

apple foo bar baz
Run Code Online (Sandbox Code Playgroud)

  • 您可以在旧的perl中使用`fc`和`Unicode :: CaseFold`模块:) (2认同)