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

Folks,

I have an array @emails with the following elements
mgr@acme.net ipadm@acme.net abuse@acme.net mgr@acme.net ipadm@acme.net abuse@acme.net

I want to remove the duplicates, so I'm using this little piece of code:
my @saw; @emails = grep(!$saw[$_]++, @emails);

When I print out @emails running -w and strict, I get the following output:
Argument "mgr@acme.net" isn't numeric in array element at ./whois.pl l +ine 64. Argument "ipadm@acme.net" isn't numeric in array element at ./whois.pl + line 64. Argument "abuse@acme.net" isn't numeric in array element at ./whois.pl + line 64. Argument "mgr@acme.net" isn't numeric in array element at ./whois.pl l +ine 64. Argument "ipadm@acme.net" isn't numeric in array element at ./whois.pl + line 64. Argument "abuse@acme.net" isn't numeric in array element at ./whois.pl + line 64. mgr@acme.net

What should I do to supress these warnings?

Thanks,
Dru
Another satisfied monk.

Replies are listed 'Best First'.
Re: Problems with Comparing Arrays
by danboo (Beadle) on Dec 28, 2001 at 02:48 UTC
    change it to:
    my %saw; # note the hash @emails = grep(!$saw{$_}++, @emails); # note the curlies
Re: Problems with Comparing Arrays
by grep (Monsignor) on Dec 28, 2001 at 02:48 UTC
    You want a hash not an array for @saw.
    #!/usr/bin/perl -w use strict; my @emails = qw/mgr@acme.net ipadm@acme.net abuse@acme.net mgr@acme.net ipadm@acme.net abuse@acme.net /; my %saw; @emails = grep(!$saw{$_}++, @emails); foreach (keys %saw) { print "$_\n"; }

    grep
    grep> cd pub 
    grep> more beer
    
Re: Problems with Comparing Arrays
by dmmiller2k (Chaplain) on Dec 28, 2001 at 03:38 UTC

    Whoops! You're using an array as if it were a hash. Are you fluent in AWK, by chance?

    Associative arrays in AWK are analagous to hashes in Perl, but their usage looks no different than that of regular arrays.

    You need to explicitly declare array @saw as a hash, e.g., %saw, and change the square brackets you are using to curly braces, as mentioned elsewhere in this thread.

    dmm

    You can give a man a fish and feed him for a day ...
    Or, you can
    teach him to fish and feed him for a lifetime