How to find which (if any) member of a list is in a given line of text

T

tinnews

Are there any cleaner/quicker ways of doing the following:-

@fs = ("string1", "string2", "string3", "string4");

foreach $f (@fs)
{
if (index ($line, $f)
{
# do something useful
}
}

Sorry if the syntax isn't quite right but I'm sure the idea is fairly
clear. The strings to search for (@fs) are read from a file once only
and are essentially constant, they can be in an array or a hash or
whatever is most efficient.

The line to search comes from standard input and is the 'variable'
in the problem.

So I'm receiving lines of text and I want to do something whenever
one of my 'constant' strings is found in that line.
 
T

Ted Zlatanov

Are there any cleaner/quicker ways of doing the following:-

@fs = ("string1", "string2", "string3", "string4");

foreach $f (@fs)
{
if (index ($line, $f)
{
# do something useful
}
}

Sorry if the syntax isn't quite right but I'm sure the idea is fairly
clear. The strings to search for (@fs) are read from a file once only
and are essentially constant, they can be in an array or a hash or
whatever is most efficient.

The line to search comes from standard input and is the 'variable'
in the problem.

So I'm receiving lines of text and I want to do something whenever
one of my 'constant' strings is found in that line.

You can also make a complicated regex (you need to qr// and then join)
to match, then study() it and maybe it will be a little faster, but
this way is probably easiest. Just put a last() inside your if()
block to skip any tests you don't need to make (if that's the right
logic).

If the search strings have commonalities, you can search for that, and
skip lines that don't contain it. I doubt it's worth the effort.
Generally, go for the simplest solution unless it's slow :)

Ted
 
T

tinnews

Ted Zlatanov said:
You can also make a complicated regex (you need to qr// and then join)
to match, then study() it and maybe it will be a little faster, but
this way is probably easiest. Just put a last() inside your if()
block to skip any tests you don't need to make (if that's the right
logic).

If the search strings have commonalities, you can search for that, and
skip lines that don't contain it. I doubt it's worth the effort.
Generally, go for the simplest solution unless it's slow :)
OK, thanks, I just wanted to be sure I wasn't missing something glaringly
obvious.
 
D

DJ Stunks

Ted said:
You can also make a complicated regex (you need to qr// and then join)
to match,

you can use Regex::presuf to form a regex from a list of strings which
generally matches faster than simply or'ing everything together...

might help. might not.

-jp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top