Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
Think about Loose Coupling
 
PerlMonks  

Re: How to look for exact pattern match

by davorg (Chancellor)
on Oct 30, 2001 at 10:22 UTC ( [id://122059]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to How to look for exact pattern match

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."

Replies are listed 'Best First'.
Re: Re: How to look for exact pattern match
by juo (Curate) on Oct 30, 2001 at 10:47 UTC

    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."

        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.

        ITYM FAQ, but yes.

        $ perldoc -q 'difference' Found in /usr/lib/perl5/5.6.1/pod/perlfaq4.pod How do I compute the difference of two arrays? How do I compute the intersection of two arrays? ...
        And since the original poster wanted case insensitivity, remember to sprinkle around lc's or uc's liberally:
        @out=grep { lc($_) ne lc($test) } @out;
        or somesuch drivel.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://122059]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.