in reply to Challenge - Creative Way To Detect Alpha Characters

I couldn't help but turn Quantum::Superpositions loose on this problem. The 'any()' function is the key to a creative solution. The idea is to compare the return value of 'any( $test_string )' with the return value of 'any( 'A'..'Z', 'a'..'z' )'. If the comparison results in equality, you've got alphas in your test string. Here's an example:

use strict; use warnings; use Quantum::Superpositions; { my $superstring = any( 'A'..'Z', 'a'..'z' ); sub has_alpha { return $superstring eq any( map chr, unpack 'C*', shift ); } } while ( <DATA> ) { chomp; print "$_\t=>\thas alphas.\n" if has_alpha($_); } __DATA__ 12345 abcdef12345 $@!^%^&(*aB&#@ 1A2B3C4D !@#$*&()

I love that module. theDamian++. Of course if you want to implement this solution in another language, you'll have to port his module too. ;)

Updated: Streamlined the code a bit, and eliminated need for split.


Dave

Replies are listed 'Best First'.
Re^2: Challenge - Creative Way To Detect Alpha Characters
by sleepingsquirrel (Chaplain) on Sep 15, 2004 at 18:14 UTC
    Of course, before porting Quantum::Superpositions to another language, you might want to consider porting perl's other nondeterministic embedded domain specific language with the terse syntax that's specifically optimized for operating on strings ;-)


    -- All code is 100% tested and functional unless otherwise noted.