Perl

Moderators: Jonathan
Number of threads: 1259
Number of posts: 3644

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Checking for matchs against array elements Posted by ve6sar on 25 Sept 2007 at 9:03 PM
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\n";
}
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\n";
}

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

Sean
Report
Re: Checking for matchs against array elements Posted by Jonathan on 26 Sept 2007 at 1:34 AM
Hi,

This line:

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

Is not doing what you think it is.

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


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.");
Report
Re: Checking for matchs against array elements Posted by ve6sar on 26 Sept 2007 at 10:11 PM
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.
:
:
: my @a = ('foo', 'bar');
: my $regex = qr/@a/;
: print $regex; # prints (?-xism:foo bar)
:
:
: 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.");




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.