Just another WTDI:
# predefine $testlist as a simple string of the set of acceptable char +acters then 'index' => sub { $test_char = chr( int( rand 256 ) ); if (index($testlist, $test_char) > -1 && length($te +st_char)==1) {} else {} },
which gives the surprisingly (to me) competitive result:
Benchmark: timing 10000000 iterations of comp, exist, index, index2, r +egex, regex2, regex3... comp: 44 wallclock secs (41.64 usr + 0.00 sys = 41.64 CPU) @ 24 +0147.93/s (n=10000000) exist: 41 wallclock secs (38.30 usr + 0.00 sys = 38.30 CPU) @ 26 +1117.06/s (n=10000000) index: 42 wallclock secs (41.30 usr + 0.00 sys = 41.30 CPU) @ 24 +2148.34/s (n=10000000) index2: 40 wallclock secs (39.48 usr + 0.00 sys = 39.48 CPU) @ 25 +3260.73/s (n=10000000) regex: 55 wallclock secs (53.28 usr + 0.00 sys = 53.28 CPU) @ 18 +7684.17/s (n=10000000) regex2: 53 wallclock secs (52.34 usr + 0.01 sys = 52.36 CPU) @ 19 +0989.13/s (n=10000000) regex3: 53 wallclock secs (50.97 usr + 0.00 sys = 50.97 CPU) @ 19 +6197.69/s (n=10000000)
I also tested a couple of variations:
regex2 - used [A-Z] in place of [A..Z] as I've never used the latter before and can't even figure what it would do at the moment
regex3 - omitted the ^ and $, which violated the spec but I wanted to know
index2 - similarly omitted the length() call

Update: Benchmarks run on NT4 PIII-500 w/256MB RAM, Perl 5.6.0, ActiveState build 618

--
I'd like to be able to assign to an luser


In reply to Re: Testing a string for a range of characters by Albannach
in thread Testing a string for a range of characters by BoredByPolitics

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.