Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Challenge - Creative Way To Detect Alpha Characters

by davido (Cardinal)
on Sep 14, 2004 at 05:22 UTC ( [id://390745]=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://390745]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (9)
As of 2024-03-28 14:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found