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")
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).
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.
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.
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.
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!
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.
I guess
@foo = ("aAa", "bbb"); @bar = grep(/^aaa/i, @foo); print join ",",@bar;
would do the trick.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With