Analyzing many $scalars for match - then action

Discussion in 'Perl Misc' started by Robert, Sep 22, 2005.

  1. Robert

    Robert Guest

    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
     
    Robert, Sep 22, 2005
    #1
    1. Advertising

  2. "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/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Sep 22, 2005
    #2
    1. Advertising

  3. Robert

    Damian James Guest

    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
     
    Damian James, Sep 22, 2005
    #3
  4. Robert

    Guest

    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
     
    , Sep 22, 2005
    #4
  5. 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.
     
    William James, Sep 22, 2005
    #5
  6. 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
     
    Nathan Wagner, Sep 22, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. bj daniels

    analyzing a csv using sql commands

    bj daniels, May 12, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    414
    bj daniels
    May 12, 2004
  2. Joe Ross
    Replies:
    0
    Views:
    599
    Joe Ross
    Aug 30, 2005
  3. mike
    Replies:
    3
    Views:
    394
  4. Replies:
    0
    Views:
    313
  5. erik
    Replies:
    18
    Views:
    203
    Gunnar Hjalmarsson
    Mar 26, 2005
Loading...

Share This Page