Analyzing many $scalars for match - then action

R

Robert

Hi,

I'm building a small subroutine that will check many variables at once for
certain matches and then take action based on it's results. Currently I am
doing it like this:

if ($name =~ /XXX/ || $email =~ /XXX/ || $inquiry =~ /XXX/ || $phone =~
/XXX/ || $comments =~ /XXX/) { &terminate; }

The the above check the 5 variables for "XXX" and if ANY of them contain
"XXX" goes to sub routine &terminate. I would like to know if there is a
more efficient/cleaner way of doing this with less code. While the above
checks 5 variables, my real script checks 22 which maks for a really long
line of code. Is there a better way to do this? For example (not real code):

if ($name,$email,$inquiry,$phone,$comments =~ /XXX/) { &terminate; }

That of course isn't real code but its cleaner and easier to manage. Thats
what I'm looking for, a better way to match many variables.

Thanx all, much appriciated.

Robert
 
A

A. Sinan Unur

I'm building a small subroutine that will check many variables at once
for certain matches and then take action based on it's results.
Currently I am doing it like this:

if ($name =~ /XXX/ || $email =~ /XXX/ || $inquiry =~ /XXX/ || $phone
=~ /XXX/ || $comments =~ /XXX/) { &terminate; }
....

For example (not real code):

if ($name,$email,$inquiry,$phone,$comments =~ /XXX/) { &terminate; }

<UNTESTED>

for( $name, $email, $inquiry, $phone, $comments ) {
terminate if /XXX/;
}

</UNTESTED>


Sinan
 
D

Damian James

I'm building a small subroutine that will check many variables at once for
certain matches and then take action based on it's results. Currently I am
doing it like this:

if ($name =~ /XXX/ || $email =~ /XXX/ || $inquiry =~ /XXX/ || $phone =~
/XXX/ || $comments =~ /XXX/) { &terminate; }

The the above check the 5 variables for "XXX" and if ANY of them contain
"XXX" goes to sub routine &terminate. I would like to know if there is a
more efficient/cleaner way of doing this with less code. While the above
checks 5 variables, my real script checks 22 which maks for a really long
line of code. Is there a better way to do this? For example (not real code):

if ($name,$email,$inquiry,$phone,$comments =~ /XXX/) { &terminate; }

That of course isn't real code but its cleaner and easier to manage. Thats
what I'm looking for, a better way to match many variables.

Thanx all, much appriciated.

[untested]

sub check {
my $pattern = shift;
for my $term ( @_ ) {
return 1 if $term =~ /$pattern/;
}
return 0
}
&terminate if check( 'XXX', $name, $email, $inquiry, $phone, $comments);

--Damian
 
M

michaelpgee

Robert said:
Hi,

I'm building a small subroutine that will check many variables at once for
certain matches and then take action based on it's results. Currently I am
doing it like this:

if ($name =~ /XXX/ || $email =~ /XXX/ || $inquiry =~ /XXX/ || $phone =~
/XXX/ || $comments =~ /XXX/) { &terminate; }

The the above check the 5 variables for "XXX" and if ANY of them contain
"XXX" goes to sub routine &terminate. I would like to know if there is a
more efficient/cleaner way of doing this with less code. While the above
checks 5 variables, my real script checks 22 which maks for a really long
line of code. Is there a better way to do this? For example (not real code):

if ($name,$email,$inquiry,$phone,$comments =~ /XXX/) { &terminate; }

That of course isn't real code but its cleaner and easier to manage. Thats
what I'm looking for, a better way to match many variables.

Thanx all, much appriciated.

Robert

Others responded with the simpler code you were looking for, but for
runtime efficiency, I think its better to concatenate all the strings
and do one pattern match.

e.g.
if ($name.$email.$inquiry.$phone.$comments =~ /XXX/) { &terminate; }

You may want to put something between the strings to prevent matching
across the end of one to the start of the next. (A null byte, or
whatever will never match.)

Mike
 
W

William James

Robert said:
Hi,

I'm building a small subroutine that will check many variables at once for
certain matches and then take action based on it's results. Currently I am
doing it like this:

if ($name =~ /XXX/ || $email =~ /XXX/ || $inquiry =~ /XXX/ || $phone =~
/XXX/ || $comments =~ /XXX/) { &terminate; }

The the above check the 5 variables for "XXX" and if ANY of them contain
"XXX" goes to sub routine &terminate. I would like to know if there is a
more efficient/cleaner way of doing this with less code. While the above
checks 5 variables, my real script checks 22 which maks for a really long
line of code. Is there a better way to do this? For example (not real code):

if ($name,$email,$inquiry,$phone,$comments =~ /XXX/) { &terminate; }

That of course isn't real code but its cleaner and easier to manage. Thats
what I'm looking for, a better way to match many variables.

Thanx all, much appriciated.

Robert

If these variables came from a string that was split into an array,
then do a search on that string.
 
N

Nathan Wagner

[snip]
For example (not real code):
if ($name,$email,$inquiry,$phone,$comments =~ /XXX/) { &terminate; }
for( $name, $email, $inquiry, $phone, $comments ) {
terminate if /XXX/;
}

Does grep in a boolean context short circuit?

terminate() if grep(/XXX/, $name,$email,$inquiry,$phone,$comments);

or would this be the same as a scalar context and test all the scalars?

Even if it doesn't short circuit, this may be more programmer efficient.
 

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,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top