in reply to Re^2: Array or Hash problem!
in thread Array or Hash problem!

Searching array without loop in Perl - is "grep" or "~~" Show some data please.

Replies are listed 'Best First'.
Re^4: Array or Hash problem!
by Anonymous Monk on Mar 04, 2010 at 18:54 UTC
    OK here is a sample of what that does:
    #!/usr/bin/perl -w my @code_array = qw(7772344 2233421 TMW2222987 TMA2222333 TMW9999988 A +MQ8873332 AMQ1111877); #the 2222987 value will be coming from the db query if ( "@code_array" =~ /(^|\s|[[:alpha:]]{0,3})2222987(\s|$)/ ) { print "Match!! \n"; } else { print "No Match \n"; }

    I hope I made it a little clear now!
      Thanks, i understand you
      #INITIAL DATA my @code_array = qw( 7772344 2233421 TMW2222987 TMA2222333 TMW9999988 AMQ8873332 AMQ1111877 ); my @test_suite = qw( 2222987 7772344 TMW2222987 TMD2222333 bbb TMA 111 ); #CODE # this print "Variant 1:\n"; for (@test_suite) { printf "%s is %s\n", $_, /^(?:\w{3})?\Q$_\E$/ ~~ @code_array ? 'match' : 'not match'; } # or that variant print "\nVariant 2:\n"; for my $val (@test_suite) { printf "%s is %s\n", $val, (grep /^(?:\w{3})?\Q$val\E$/, @code_array) ? 'match' : 'not match'; }
      Resulting:
      
      Variant 1:
      2222987 is match
      7772344 is match
      TMW2222987 is match
      TMD2222333 is not match
      bbb is not match
      TMA is not match
      111 is not match
      
      Variant 2:
      2222987 is match
      7772344 is match
      TMW2222987 is match
      TMD2222333 is not match
      bbb is not match
      TMA is not match
      111 is not match