in reply to Checking Arrays
You are recreating the functionality of grep.
Where you are using grep, that should be just a regexp or (even better) index. But back to the point, grep works on a list and returns a list of matches
#!/usr/bin/perl -w use strict; my @lines = ( 'this is line one', 'this is line TWO', 'this is line three', 'this is line four perl', 'this is line fiveperl', 'perl' ); my @search = grep { index($_,'perl')+1 } @lines; # my @search = grep {/perl/} @lines; # # if you don't want line five use /\bperl\b/; print "TRUE\n" if (@search); foreach (@search) { print "$_\n"; }
If you do use a $var to populate your regexp please use quotemeta
As for your other question 'if (@array)' will return true if there are any elements in the array (even if the only element is 0 or undef)
UPDATE: BTW here is the benchmark on the re vs. index solution
Benchmark: timing 100000 iterations of index, re... index: 4 wallclock secs ( 3.54 usr + 0.00 sys = 3.54 CPU) @ 28 +248.59/s (n=100000) re: 7 wallclock secs ( 6.98 usr + 0.00 sys = 6.98 CPU) @ 14 +326.65/s (n=100000)
| Mynd you, mønk bites Kan be pretti nasti... |
|
|---|