Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Checking for matchs against array elements

ve6sarve6sar Posts: 11Member
Hi

Here's what I'm tring to do. When I start my script it reads a text file and puts the line into an array @calls

Then it opens a socket connection to a remote server and takes data and I want it to test to see if any of the elements of the array match the first part of the lines coming from the remote server. This is the code I've tried but it doesn't work. ($a is the data coming from the socket connection)

if ($a =~ m/@calls/)
{
processdata ($a);
}
else
{
print "no match
";
}
but if I use this code the script works but only for one site.
my $call = "WEBBER"

if ($a =~ m/$call/)
{
processdata ($a);
}
else
{
print "no match
";
}

I'm sure it's simple but it's eluding me....

Sean

Comments

  • JonathanJonathan Posts: 2,914Member
    Hi,

    This line:

    : if ($a =~ m/@calls/)

    Is not doing what you think it is.

    [code]my @a = ('foo', 'bar');
    my $regex = qr/@a/;
    print $regex; # prints (?-xism:foo bar)[/code]

    Basically, when you interpolate an array you do not (in Perl 5) get an alternation, just a space separated list of all the strings. So it won't match as you want it to. You could do something more like:

    my $calls_alt = join('|', @calls);

    And later:

    if ($a =~ m/$calls_alt/o) # o option = compile once

    Or perhaps safer (the regex solution breaks if any elements of your array are metacharacters) here's a non-regex solution:

    if (grep { $_ eq $a } @calls)

    Which is what I'd probably have done.

    Jonathan
    ###
    for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");
  • ve6sarve6sar Posts: 11Member
    Thanks for your help I couldn't get the grep methode to work but the regex works great. There shouldn't be any metacharactors to worry about.

    I'm slowly learnig Perl, thanks again for your help.

    Sean

    : Hi,
    :
    : This line:
    :
    : : if ($a =~ m/@calls/)
    :
    : Is not doing what you think it is.
    :
    : [code]: my @a = ('foo', 'bar');
    : my $regex = qr/@a/;
    : print $regex; # prints (?-xism:foo bar)[/code]:
    :
    : Basically, when you interpolate an array you do not (in Perl 5) get
    : an alternation, just a space separated list of all the strings. So
    : it won't match as you want it to. You could do something more like:
    :
    : my $calls_alt = join('|', @calls);
    :
    : And later:
    :
    : if ($a =~ m/$calls_alt/o) # o option = compile once
    :
    : Or perhaps safer (the regex solution breaks if any elements of your
    : array are metacharacters) here's a non-regex solution:
    :
    : if (grep { $_ eq $a } @calls)
    :
    : Which is what I'd probably have done.
    :
    : Jonathan
    : ###
    : for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    : (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    : /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

Sign In or Register to comment.