|Perl: the Markov chain saw|
Answer: How do I compare two strings against two arrays respectively?by Russ (Deacon)
|on Nov 04, 2000 at 20:42 UTC||Need Help??|
Q&A > strings > How do I compare two strings against two arrays respectively? - Answer contributed by Russ
Yes. You just need to call grep twice (you had incorrect syntax, by leaving off the second grep)
You want to use not (or '!') on the second grep statement (rather than in it (ne) because you want to know if the second array does not contain $sec_arg. What you had would have told you if every line in @anotherarray contained $sec_arg.
Clear as mud? :-)
grep is not in void context in this case, because we are testing the scalar value of the array returned by grep. Each grep returns us an array of the lines containing the test string. In scalar context, that becomes the number of elements in that list. If there are no elements in the list, we didn't find the string in @Array, and the statement is false.
This is not the most CPU-efficient way to accomplish your task, but it is the most PROGRAMMER-efficient way. Weigh the benefits accordingly. If you will be testing for very many strings (in my opinion >1 is enough), you should use one of the other methods (like building a hash, as suggested by runrig).
P.S. Based on your question, it sounds like, for each string, you want to find it in the first array. If so, then check the second array for that string. If the string does not appear in the first array, die. Right?
If you want to know that both strings are good names before you then check both strings against interfaces: