Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

regex match in array

by rsiedl (Friar)
on May 27, 2004 at 13:00 UTC ( [id://356881]=perlquestion: print w/replies, xml ) Need Help??

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

Hi

I know how to check if a value exists in an array
( {map { $_ => 1 } @myTerms}->{"term"} )
But i am wondering if its possilbe to do this with a regex?
i.e. ( {map { $_ => 1 } @myTerms}->{/TE(.)M/i} )
I've tried the above but it hasn't worked. Anyone know?

Cheers,
Reagen

Replies are listed 'Best First'.
Re: regex match in array
by dave_the_m (Monsignor) on May 27, 2004 at 13:17 UTC
    ( {map { $_ => 1 } @myTerms}->{"term"}
    That's a hard way to do it. Try this.
    grep $_ eq "term", @myTerms
    Then doing the regex comes out trivially:
    grep /TE(.)M/i, @myTerms

    Dave.

      Cool, thanks.

      Thats just a bit easier :)
      Not sure how I should do this but i have a follow-up question. Should it be posted as a new node or in reply...?
      Anyway this time I'll do it as a reply and if it is the wrong thing just let me know.

      The question is:
      How can i get the match to match at least a whole item from the array?
      i.e.
      #!/usr/bin/perl use strict; use warnings; my @myTerms = ( 'blah foo', 'test' ); my $term = "blah"; if ( grep /\Q$term\E/, @myTerms) { print "$term does match\n"; } # end-if exit;
      will match, but i would only like it to match if the _term_ is /^blah foo(.*)/
      Could you help me out?
      Cheers,
      Reagen
Re: regex match in array
by Chady (Priest) on May 27, 2004 at 13:16 UTC

    That's a really wierd way to search for elements in an array. But if you really want to do it that way, use grep in void context.

    ( grep { /TE(.)M/i } @myTerms )
    /me hides away

    He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

    Chady | http://chady.net/
Re: regex match in array
by Limbic~Region (Chancellor) on May 27, 2004 at 13:37 UTC
    rsiedl,
    In Perl, we have the motto "There Is More Than One Way To Do It". Variations of this particular problem come so often, that there are lots of solutions. Converting to a hash is a great solution if you consider a few points:
    • I will need to verify multiple values
    • The array won't change between checks
    • I only need to know if the value appears once
    • Duplicating the array as a hash will not be a memory concern

    If one or more of these requirements do not fit your situation, then grep is a great alternative. By nature it processes the entire list even if a match was found on the first item - this allows you to determine how many times an item appears in the array. It is also relatively fast since it is a built-it function.

    The third choice that I am presenting is actually my favorite. List::Util has first which will return true on first match or undef if the item is not found in the list. It allows you to pass a code reference as the argument which allows you to provide a regex, an exact match, or some custom Frankestein monster as the determining agent.

    Cheers - L~R

Re: regex match in array
by EdwardG (Vicar) on May 27, 2004 at 13:18 UTC
    perldoc -q element

    How can I tell whether a certain element is contained in a list or array?

    Hearing the word "in" is an *in*dication that you probably should have used a hash, not a list or array, to store your data. Hashes are designed to answer this question quickly and efficiently. Arrays aren't.

    That being said, there are several ways to approach this. If you are going to make this query many times over arbitrary string values, the fastest way is probably to invert the original array and maintain a hash whose keys are the first array's values.

    @blues = qw/azure cerulean teal turquoise lapis-lazuli/; %is_blue = (); for (@blues) { $is_blue{$_} = 1 }

    Now you can check whether $is_blue{$some_color}. It might have been a good idea to keep the blues all in a hash in the first place.

    Update: misread the question, sorry

     

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://356881]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-04-20 03:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found