[Gllug] Regexp
Tom Gilbert
tom at linuxbrit.co.uk
Thu Mar 7 19:29:22 UTC 2002
* Jackson, Harry (HJackson at colt-telecom.com) wrote:
>
>
> > -----Original Message-----
> > From: Alain Williams [mailto:addw at phcomp.co.uk]
> >
> > How about
> >
> > $namelist = join ':', @name;
> > if/^$namelist/
> >
> > Or do you want to do something completely different ?
>
> Sorry I was a bit vague. I have a list of items in an array and each one of
> these items are a valid match.
>
> @name = ("E1X","Z42","F11","34F");
> @name2 = ("EX","Z242","F1wdf1","32344F");
>
> The arrays are quite big and the letters bear no relation to each other. I
> then want to check if these letters will appear in a line and output if
> possible.
my @name = ("E1X","Z42","F11","34F");
my $line = "test F11 bar 34F foo\n";
print "matched $line" if (map { $line =~ /$_/ } @name);
If you want to use both the lists you showed above, replace @name with
(@name, @name2).
Fine tune the /$_/ regexp if you want a more specific match (only match
whole words, or maybe you want it case insensitive). If you don't need a
regexp and just want to see if it's there, it would be faster to use
index(), eg:
print "matched $line" if (map { index($line, $_) } @name);
You might want to look at perldoc -f grep if you want a similar but I
guess opposite effect (one test on multiple lines).
Tom.
--
.^. .-------------------------------------------------------.
/V\ | Tom Gilbert, London, England | http://linuxbrit.co.uk |
/( )\ | Open Source/UNIX consultant | tom at linuxbrit.co.uk |
^^-^^ `-------------------------------------------------------'
--
Gllug mailing list - Gllug at linux.co.uk
http://list.ftech.net/mailman/listinfo/gllug
More information about the GLLUG
mailing list