I'm not good with perl but I've been told that I have to make this project in perl. Sorry for all of the questions.
Here is my question: Is there a way in PERL to determine if a string is in an array? Basically search an array for a string?
Is $string in @array1?Code:@array1 = ("Bnn","Cnn","Dnn"); $string = "Cnn";
Code:for (int i;;) { cout << "Smith"; }
I don't know perl, but in most languages you can loop through the index range of the array and compare each string in the array.
one way:
but if @array1 is big, tens of thouands or more of elements, you might use a loop and "last" to break out early on the first true match. But if its just a few hundred/thousand strings, this way will be fine.Code:@array1 = ("Bnn","Cnn","Dnn"); $string = "Cnn"; if (grep {$_ eq $string} @array1) { print "Found $string\n"; } else { print "Nope, no $string\n"; }
There are currently 2 users browsing this thread. (0 members and 2 guests)
Bookmarks