Re: Array help almost correct

From: Chris Charley (charley_at_pulsenet.com)
Date: 05/26/04


To: <beginners@perl.org>
Date: Wed, 26 May 2004 16:23:17 -0400


[snip]

> my $acceptable = qr/^(?:sp|lp|ep12|ffp|vid|dvd|bx|bkk|cd|csd)$/;
>
> and then using $acceptable to do the matchings, such as
>
> grep /$acceptable/, @array
>
>
> it would be faster and more readable :-)

If the acceptable list is that large, using a hash would be TMTOWTDI :-)

#!/usr/bin/perl
use strict;
use warnings;

my %acceptable = map {$_ => 1} qw/ sp lp ep12 ffp vid dvd bx bkk cd csd /;

my @a = ([qw/ cd csd cd /], [qw/ abc ed csd cd /]);

for (@a) {
 if (grep {not $acceptable{$_}} @$_) {
  print "Not acceptable\n";
 }
 else {
  print "OK\n";
 }
}

Chris



Relevant Pages

  • Re: Hash of hashes?
    ... The key of this anonymous hash is $genotype? ... $snp is a scalar ... alleles (keys of the second anonymous hash). ...
    (perl.beginners)
  • Re: How to implement a Hash Table in C
    ... if you don't do that they need to be provided as queue operations. ... probing completely, or I have. ... hash value h has so far resulted in n collisions in a table of size s, ...
    (comp.lang.c)
  • Re: hashs and symbolic refrences
    ... [snip more of the same] ... To just add another key/value pair (and auto-vivify the hash reference the ... > I found that I was using symbolic references quite by accident, ... The problem is that I am capturing only one tag ...
    (comp.lang.perl.misc)
  • Re: [fw-wiz] Mis-attribution - Re: How automate firewall tests
    ... Hash: SHA1 ... On Mon, 13 Nov 2006, Chris Blask wrote: ... SSN - [SNIP] ...
    (Firewall-Wizards)
  • Re: Changes to php{4,5}
    ... > Hash: SHA1 ... > Is it just me, or have the changes introduced in the phpports ...
    (comp.unix.bsd.freebsd.misc)