To check whether some element of @lines
array matches,
if (grep /"success"/, @lines) {
Question
hi i tried find $#lines include succcess or not include in perl. But im sure $#lines include success but its returns not include. what is wrong with my code ?
if ( $#lines =~ /"success"/ ) {
print "Found success\n";
} else {
print "Did not find success\n";
Solution 2
To check whether some element of @lines
array matches,
if (grep /"success"/, @lines) {
OTHER TIPS
The expression:
if ( $#lines =~ /"success"/ ) { ...
will always fail, since $#lines
is the last index (zero-based) of @lines
. Thus, if @lines
had 100 lines (indexes 0 through 99), your expression would effectively be:
if ( 99 =~ /"success"/ ) { ...
Use mpapec's solution. However, if you're only looking for success
, and the word's not enclosed by double-quotes, then use /success/
. And if you want a case-insensitive match, use the i
modifier: /success/i
.
Hope this helps!