
mhx
Enthusiast
/ Moderator
Oct 15, 2001, 10:42 PM
Post #3 of 14
(1151 views)
|
|
Re: Matching an item in an array
[In reply to]
|
Can't Post
|
|
I don't know why this isn't working for you. The following example code worked when I ran it:
#!/bin/perl -w @bannedlist = ("Tom", "Dick", "Harry"); $last = "Dick"; name_check(); sub name_check { foreach $banned (@bannedlist) { if ($banned =~ /$last/) { print "banned $last!\n"; } } } However, you should try not to use a regex match to compare strings. In the above case, $banned="Dicky" and $last="Dick" would ban Dick if you only wanted to ban Dicky. So you should rather use eq or put /^anchors$/ around your regex. In combination with Perl's grep function, this would be:
#!/bin/perl -w @bannedlist = ("Tom", "Dick", "Harry"); $last = "Dick"; name_check(); sub name_check { if( grep /^$last$/, @bannedlist ) { print "banned $last!\n"; } } Hope this helps. -- Marcus
s$$ab21b8d15c3d97bd6317286d$;$"=547269736;split'i',join$,,map{chr(($*+= ($">>=1)&1?-hex:hex)+0140)}/./g;$"=chr$";s;.;\u$&;for@_[0,2];print"@_,"
|