Checking for matchs against array elements - Programmers Heaven

Howdy, Stranger!

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

Categories

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.