Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I search a Perl array for a matching string?

What is the smartest way of searching through an array of strings for a matching string in Perl?

One caveat, I would like the search to be case-insensitive

so "aAa" would be in ("aaa","bbb")

like image 430
Mike Avatar asked May 27 '10 22:05

Mike


People also ask

How do I find a string in an array in Perl?

The Perl grep() function is a filter that runs a regular expression on each element of an array and returns only the elements that evaluate as true. Using regular expressions can be extremely powerful and complex. The grep() functions uses the syntax @List = grep(Expression, @array).

How do I match an array pattern in Perl?

m operator in Perl is used to match a pattern within the given text. The string passed to m operator can be enclosed within any character which will be used as a delimiter to regular expressions.

How do I check if all elements in an array are the same in Perl?

You can check how many times the element in the array (@test) is repeated by counting it in a hash (%seen). You can check how many keys ($size) are present in the hash (%seen). If more than 1 key is present, you know that the elements in the array are not identical.

How do you match elements in an array?

Today, you'll learn a useful trick to find all matching items in an array by using the Array. filter() method. The Array. filter() method creates a new array by iterating over all elements of an array and returns those that pass a certain condition as an array.

How do you get the matching element in an integer array?

All you need to do is iterate over the pairs of key,val multiply each key*val then sum up the multiplied values and you get your correct total. And if you need just the number of matched, you can in another variable sum up just the vals . I hope this helps!


2 Answers

It depends on what you want the search to do:

  • if you want to find all matches, use the built-in grep:

    my @matches = grep { /pattern/ } @list_of_strings; 
  • if you want to find the first match, use first in List::Util:

    use List::Util 'first';   my $match = first { /pattern/ } @list_of_strings; 
  • if you want to find the count of all matches, use true in List::MoreUtils:

    use List::MoreUtils 'true'; my $count = true { /pattern/ } @list_of_strings; 
  • if you want to know the index of the first match, use first_index in List::MoreUtils:

    use List::MoreUtils 'first_index';  my $index = first_index { /pattern/ } @list_of_strings; 
  • if you want to simply know if there was a match, but you don't care which element it was or its value, use any in List::Util:

    use List::Util 1.33 'any'; my $match_found = any { /pattern/ } @list_of_strings; 

All these examples do similar things at their core, but their implementations have been heavily optimized to be fast, and will be faster than any pure-perl implementation that you might write yourself with grep, map or a for loop.


Note that the algorithm for doing the looping is a separate issue than performing the individual matches. To match a string case-insensitively, you can simply use the i flag in the pattern: /pattern/i. You should definitely read through perldoc perlre if you have not previously done so.

like image 93
Ether Avatar answered Sep 17 '22 14:09

Ether


I guess

@foo = ("aAa", "bbb"); @bar = grep(/^aaa/i, @foo); print join ",",@bar; 

would do the trick.

like image 31
Peter Tillemans Avatar answered Sep 16 '22 14:09

Peter Tillemans