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

How can I look for an exact pattern match. I would like to interrogate a pattern and remove exact matches out of the array which occur in another array. If I use (grep s/$temp/ /ig,@new_array) then it will remove all matches instead of exact matches. See example below. Any clues ?

use strict; my @array = qw (bga); my @new_array = qw (bga cbga test); print "@array\n"; foreach my $temp(@array) { chomp($temp); my $tester = grep s/$temp/ /ig,@new_array; } print "@new_array\n";

Replies are listed 'Best First'.
Re: How to look for exact pattern match
by davorg (Chancellor) on Oct 30, 2001 at 15:22 UTC

    grep doesn't do what you think it does. For a start you're assigning the return value to a scalar, but it returns a list.

    I think that what you want is something like this:

    my @in = qw(bga cbga test); my @out; my @test = qw(bga); foreach my $test (@test) { push @out, grep { /$test/ } @in; }

    But I'm slightly worried by your phrase "exact pattern match". Perhaps what you really want is:

    my @in = qw(bga cbga test); my @out; my @test = qw(bga); foreach my $test (@test) { push @out, grep { $_ eq $test } @in; }
    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you don't talk about Perl club."

      It's not really what I like to do. I want to remove all exact matches which occur in @test out of the array @in. So the array @in should become (cbga test). With (grep s/($temp)/ /ig,@new_array) it removed all matches instead of exact matches so my array became (c test) instead of (cbga test).

        You're not removing the elements, you're replacing them with spaces.

        I think you want this, then:

        my @in = qw(bga cbga test); my @out = @in; my @test = qw(bga); foreach my $test (@test) { @out = grep { $_ ne $test } @out; }

        Which leave @out contains all the elements from @in that don't exist in @test.

        Actually, thinking about it, that's the disjunction of two sets and there's probably some really short recipe to do it in the Cookbook.

        --
        <http://www.dave.org.uk>

        "The first rule of Perl club is you don't talk about Perl club."

Re: How to look for exact pattern match
by arhuman (Vicar) on Oct 30, 2001 at 16:04 UTC
    Is the following code what you're looking for ?
    use strict; my @array = qw (bga); my @new_array = qw (bga cbga test); my @tester; print "@array\n"; # #foreach my $temp(@array) { # chomp($temp); # @tester = grep !/^$temp$/ig,@new_array; #} my $temp='('.(join'|',@array).')'; @tester = grep !/^$temp$/ig,@new_array; print "@tester\n";

    UPDATE :
    Ooops davorg said it faster...
    Furthermore if it's case-sensitive 'ne' is better than the regex used in my examples...

    UPDATE2 :
    Thanks Davorg for catching my error !
    Only the last element of array will be taken out of the list,
    as the @tester array is completly reconstructed at each step of the loop (only ok with one element ;-)
    I've corrected the code with another solution...
    But you'd definitly better use davorg's one.

    "Only Bad Coders Code Badly In Perl" (OBC2BIP)