in reply to Challenge - Creative Way To Detect Alpha Characters

After reading posts, I realized my error in using split, so I rewrote and simplified to:

while ($_ = substr($foo,$i++,1)){ if (++$_**2 == 0) { print "$foo Contains Alpha Characters\n"; last; } }

This is better I think.

Cameron

UPDATE: I have updated the code with the suggestions offered by ambrus. Thanks for pointing that out...ambrus I reverted to the original code, and am posting the changes below.

Updated with ambrus suggestions:
my $i = 0; while (local $_ = substr($foo,$i++,1)){ if (++$_ == 0) { print "$foo Contains Alpha Characters\n"; last; } }


I optimized further:

while (++substr($foo,$i++,1) != 0){}
This breaks at an alpha char, and significantly increases the speed, but it is still ~3 times slower that initial solution.

Replies are listed 'Best First'.
Re^2: Challenge - Creative Way To Detect Alpha Characters
by ambrus (Abbot) on Sep 14, 2004 at 15:47 UTC

    I think this works without the **2 too.

    Also, you'll want to add my $i = 0; before and change while ($_ = to while (local $_ = (or reorganize it to a for loop that localizes automatically) if you want to use this more than once.

    Update: for the record, the original code had ++$_**2 instead of ++$_.