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

Hi All
I have two data files that I want to reconcile. For ease, I have loaded these into two arrays. So basically, I'm trying to find any items in one of the arrays that arent in the second array (and print 1 message if any differences...)
I tried  print STDERR "DEBUG YYY \n" unless map {grep \$_\, @ingfile} @sybfile; But it doesn't quite work? Suggestions etc... Please.

Replies are listed 'Best First'.
Re: Checking for array entry in another array
by RhetTbull (Curate) on Jul 26, 2001 at 19:42 UTC
Re: Checking for array entry in another array
by PrakashK (Pilgrim) on Jul 26, 2001 at 20:13 UTC
    Hash slices can be handy here. I am using the example arrays from the categorized answer How can I find the union/difference/intersection of two arrays? Just replace @simpsons and @females with your arrays.
    my @simpsons=("homer","bart","marge","maggie","lisa"); my @females=("lisa","marge","maggie","maude"); my %simpsons_hash; @simpsons_hash{@simpsons} = @simpsons; @female_simpsons = grep {$_} @simpsons_hash{@females};
    HTH.
Re: Checking for array entry in another array
by Kevman (Pilgrim) on Jul 26, 2001 at 19:28 UTC
    Apologies.

    Obviously the code should read  print STDERR "DEBUG YYY \n" unless map {grep /$_/, @ingfile} @sybfile;

      It won't work because map and grep both localise $_: inside the braces, $_ is aliased to each element of @sybfile in turn. But, grep also aliases $_ to each element of @ingfile, which hides the former $_.

      In fact, you end up doing $_ =~ /$_/, which is pretty much always true. You have to save the map's $_ into some temporary variable:

      print STDERR "DEBUG YYY\n" unless map { my $tmp = $_; grep /\Q$tmp\E/, @ingfile; } @sybfile;

      HTH

      --bwana147