Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Looking for exact match in an array

by juo (Curate)
on Dec 10, 2004 at 05:54 UTC ( #413759=perlquestion: print w/replies, xml ) Need Help??
juo has asked for the wisdom of the Perl Monks concerning the following question:

When looking for a match in a pattern I always used the grep function, however using this function has the limitation that it does not look for an exact match but rather for a pattern match. If I look for ACB and I have in my array ACBD it will match it while I don't want it to be matched. Anybody has a good idea on this without changhing to much of the routine as I like it that short.

if (not grep($_ eq $machine_sheetname,@sheets)) { push @sheets,$machine_sheetname; $skip = 0; }

Replies are listed 'Best First'.
Re: Looking for exact match in an array
by saintmike (Vicar) on Dec 10, 2004 at 06:01 UTC
    grep { $_ eq "foo" } @choices
    will return only elements of @choices exactly matching the string "foo". There's no pattern matching involved whatsoever.
Re: Looking for exact match in an array
by gaal (Parson) on Dec 10, 2004 at 06:25 UTC
    saintmike answered your question. But by the way, it looks like you want a data structure you can easily and efficiently use to determine membership of a particular value. In this case, a hash can be useful:

    my %sheets; [...] # The next three methods are equivalent. Pick whichever you like most. # 1 if (not exists $sheets{$machine_sheetname}) { $sheets{$machine_sheetname} = 1; # "1" is just a dummy existenc +e flag } # 2 $sheets{$machine_sheetname} = 1 unless $sheets{$machine_sheetname}; # 3 $sheets{$machine_sheetname} ||= 1;

    Then, when you need all the sheets, use this expression:

    keys %sheets;

    The prime limitation of this approach, however, is that hashes are not sorted. If the order of your sheets matter, this may not be appropriate after all, or, if you do want to use them, you need to use something like Tie::IxHash.

      Nice reply, but there are a few things I would do differently. Consider this another "[p]ick whichever you like most" post.

      # your number 1 uses "if (not ...)" # which is equivalent to "unless (...)" # 4 unless (exists $sheets{$machine_sheetname}) { $sheets{$machine_sheetname} = 1; } # but I'm not really sure why you're checking for existence at all. # just incrementing the value is the simplest approach. # 5 $sheets{$machine_sheetname}++;
        Personally, I like unless almost only in statement modifier position, where the expression is simple. This is a matter of taste and I'm not arguing that this is better. (Well, I'm almost certain to wrinkle my nose at *complex* expressions that start with unless.) So I'd much rather do #2 than #4. You'll notice that I included the existence test only in #1, which was the long form.

        #5 is fine and dandy, and has the additional feature of giving you a count of seen instances. I do this sometimes :) but where what I'm implemented is strictly a set, sometimes I prefer decidedly not to have it, just to make clear that the only information this hash provides is membership. YMMV etc.

Re: Looking for exact match in an array
by pg (Canon) on Dec 10, 2004 at 05:56 UTC

    use ^ and $ to match the two ends: /^ACB$/

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://413759]
Approved by davido
[Corion]: Meh ... Minion really doesn't want to run under the fork() emulation on Windows (which I understand), but has no other way either (which I find inconvenient)
[Corion]: Other than that, it seems really polished

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2018-06-21 15:01 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (118 votes). Check out past polls.