user1681502
user1681502

Reputation:

Case Insensitive Grep?

I am using this grep statement in PERL to find non-duplicated names. Can anyone tell me how to make it case-insensitive? I know I need to use an 'i', but I'm not sure where it goes. Thanks!

@nondup = grep {$marked{$_}++; $marked{$_} = 1;} @names

Upvotes: 0

Views: 484

Answers (1)

Miller
Miller

Reputation: 35208

Use either fc (Perl 5.16 or newer) or 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";

Outputs:

apple foo bar baz

Upvotes: 5

Related Questions