Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Analyzing many $scalars for match - then action

Reply
Thread Tools

Analyzing many $scalars for match - then action

 
 
Robert
Guest
Posts: n/a
 
      09-22-2005
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


 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      09-22-2005
"Robert" <> wrote in
news:YlnYe.539209$s54.79301@pd7tw2no:

> 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


--
A. Sinan Unur <>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html
 
Reply With Quote
 
 
 
 
Damian James
Guest
Posts: n/a
 
      09-22-2005
On Thu, 22 Sep 2005 00:59:36 GMT, Robert said:
> 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
 
Reply With Quote
 
michaelpgee@gmail.com
Guest
Posts: n/a
 
      09-22-2005

Robert wrote:
> 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

 
Reply With Quote
 
William James
Guest
Posts: n/a
 
      09-22-2005

Robert wrote:
> 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.

 
Reply With Quote
 
Nathan Wagner
Guest
Posts: n/a
 
      09-22-2005
On 2005-09-22, A. Sinan Unur <> wrote:
> "Robert" <> wrote in


[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.

--
Nathan Wagner
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
check differnet pattern match then perform an action mike Java 3 09-18-2008 07:06 AM
Help. SessionID is x then y then x then y BodiKlamph@gmail.com ASP General 0 09-03-2005 03:02 PM
Analyzing Requirements and Defining Microsoft .NET Solution Architectures Rohit Wason MCSD 5 12-27-2004 10:49 AM
.NET vs 6.0: Analyzing Requirements & Defining Solution Architectures Jamie Thomson MCSD 3 11-29-2003 09:34 AM
Books for 70-300 Analyzing Requirements Anand MCSD 2 10-13-2003 05:20 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57