in reply to Most efficient way of search elements in an array

So close, yet so far :)

If you're using Perl 5.10, you can use the "Smartmatch Operator" ~~ instead of =~ and your program will work:

use strict; for my $testcase ([1,1,1,0,1],[1,1,1,1,1],[1,10,101]) { my @results_file = @$testcase; if (@results_file ~~ m/0/) { print"\n--FAIL-- @results_file"; } else { print "\n--PASS--STATUS\n"; print "\n@results_file\n"; } }

The only relevant change is from @results_file =~ /0/ to @results_file ~~ /0/. You might want to consider anchoring your match, so you match only a 0, and not 100, or 101.

Replies are listed 'Best First'.
Re^2: Most efficient way of search elements in an array
by AnomalousMonk (Archbishop) on Sep 15, 2009 at 15:06 UTC
    But Smartmatch is smart enough to match on numeric equality:
    >perl -wMstrict -le "for my $ar ( [1, 1, 0, 1], [0], [0, 0, 0], [qw(0)], [qw(00)], [qw(000)], [qw(1 1 0 1)], [1], [], [1, 10, 01, 101, 010], [qw(1 10 01 101 010)], ) { print qq{(@$ar): }, @$ar ~~ 0 ? 'naught' : 'not' } " (1 1 0 1): naught (0): naught (0 0 0): naught (0): naught (00): naught (000): naught (1 1 0 1): naught (1): not (): not (1 10 1 101 8): not (1 10 01 101 010): not
    Update: Added 'numeric string' test cases.