Baratski has asked for the wisdom of the Perl Monks concerning the following question:

Hello. I'm using the following to remove duplicates from an array.
I'm having a problem removing duplicates that are different in case, but the "same", ie; Bob Jones & bob jones.

@allnames = grep(!$saw{$_}++, @names);

Is there a way to accomplish this feat?

Replies are listed 'Best First'.
Re: Remove equal but different duplicates from array
by ysth (Canon) on Jul 23, 2004 at 19:49 UTC
    As the key, use something that would be the same for both your duplicates, e.g. grep !$saw{lc $_}++, @names.

    Or perhaps the more complicated: grep {(my $temp = lc $_) =~ y/a-z//cd; !$saw{$temp}++} @names

Re: Remove equal but different duplicates from array
by Eimi Metamorphoumai (Deacon) on Jul 23, 2004 at 19:49 UTC
    Just force all the entries to lowercase before comparing them.
    @allnames = grep(!$saw{lc}++, @names);
    Update: See L~R below.
      Eimi Metamorphoumai,
      While lc works on $_ if not specified, you can't put it in hash brackets and expect it not to be stringified (battle of DWYMisms). To make it work change it to:
      @allnames = grep(!$saw{lc()}++, @names); # or @allnames = grep(!$saw{lc $_}++, @names);

      Cheers - L~R

        Thanks everyone.

        I used:

        @allnames = grep(!$saw{lc $_}++, @names);

        Worked great!