Home > Archive > PERL Miscellaneous > September 2005 > Analyzing many $scalars for match - then action
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Analyzing many $scalars for match - then action
|
|
| Robert 2005-09-21, 9:56 pm |
| 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. Sinan Unur 2005-09-21, 9:56 pm |
| "Robert" <rbutcher.nospam@hotmail.com> 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 <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)
comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/c...guidelines.html
| |
| Damian James 2005-09-21, 9:56 pm |
| 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
| |
| michaelpgee@gmail.com 2005-09-21, 9:56 pm |
|
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
| |
| William James 2005-09-22, 3:57 am |
|
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.
| |
| Nathan Wagner 2005-09-26, 6:58 pm |
| On 2005-09-22, A. Sinan Unur <1usa@llenroc.ude.invalid> wrote:
> "Robert" <rbutcher.nospam@hotmail.com> wrote in
[snip]
[color=darkred]
> 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
| |
| Jim Gibson 2005-09-26, 6:58 pm |
| In article <slrndj5lb4.o1p.nw@granicus.if.org>, Nathan Wagner
<nw@hydaspes.if.org> wrote:
> On 2005-09-22, A. Sinan Unur <1usa@llenroc.ude.invalid> wrote:
>
> [snip]
>
>
>
> 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.
Judging from the documentation for grep:
"Evaluates the BLOCK or EXPR for each element of LIST (locally
setting $_ to each element) and returns the list value consist-
ing of those elements for which the expression evaluated to
true. In scalar context, returns the number of times the
expression was true."
I would guess that it does not short circuit, else how could it return
the total number of matches?
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
|
|
|
|
|