Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Challenge - Creative Way To Detect Alpha Characters

by ambrus (Abbot)
on Sep 14, 2004 at 07:30 UTC ( [id://390765]=note: print w/replies, xml ) Need Help??


in reply to Challenge - Creative Way To Detect Alpha Characters

This is both perl-specific and slow, but I'll just show it:

sub isalpha { no warnings "numeric"; my($p) = @_; 0==++$p; } sub hasalpha { my($s) = @_; isalpha(substr($s, 0, 1, "")) and return 1 while length($s); return; };

Replies are listed 'Best First'.
Re^2: Challenge - Creative Way To Detect Alpha Characters
by Pragma (Scribe) on Sep 14, 2004 at 07:37 UTC
    You could hit it from the back {g}:
    sub hasalpha { local($_) = @_; isalpha chop and return 1 while length; return; }
Re^2: Challenge - Creative Way To Detect Alpha Characters
by Jasper (Chaplain) on Sep 14, 2004 at 10:17 UTC
    Shurely shome mishtake. This returns true for anything other than 0-9, doesn't it? Is that what the OP wanted?

    My solution, slightly similar to one of the others:
    $range[ chop ] = 1 while $_; # $_ is copy :) grep $_, @range[65..90,97..122];
    Did someone mention efficiency (I hope not)?

      No. The isalpha function I gave returns true only for [a-zA-Z] characters, false on numbers, punctation, international letters, or any other character.

      It seems to give the same results for me than lc ne uc:

      sub isalpha { no warnings "numeric"; my($p) = @_; 0==++$p; } sub hasalpha { my($s) = @_; isalpha(substr($s, 0, 1, "")) and return 1 while length($s); return; }; @t = ("f/2", "-2/", "*+)", "165", "foop", " A="); $\=$/; $,=" "; print grep hasalpha($_), @t; print grep lc ne uc, @t;

      outputs

      f/2 foop A= f/2 foop A=

      This is because if $p is a non-alnum character in the isalpha function, $p++ converts it to a numeric 0 first, than it increases it to 1, so 0==$p++ is false.

      Your forgot to ord before you chop.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2024-04-18 05:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found