SCOUG-Programming Mailing List Archives
Return to [ 17 | 
September | 
1998 ]
<< Previous Message << 
 
 
 
Content Type:   text/plain 
> > Is there a clean way to collect all of the  
> > numeric data from a string.  For example, given:  
> >     abc123def456ghi  
> > I want  
> >     123456  
 
I should have also mentioned that, if you want to eliminate _every_  
character except 0..9, use XRANGE() to generate your inline string.  
 
For ASCII display characters:  
 
    st = 'abc123def456ghi'  
    say space( translate( st, '', xrange(' ','/') || xrange(':','~')),  
0)  
 
or, for all binary characters,  
 
    st = 'abc123def456ghi'  
    say space( translate( st, '', xrange('00'x,'2f'x) ||  
xrange('3a'x,'ff'x)), 0)  
 
How it works:  XRANGE() generates a string of characters  
('abcdefghij...') that you don't want.  The concatenation ("||") puts  
the XRANGE() character strings together.  Then TRANSLATE() replaces  
those characters with spaces.  Finally, SPACE(,0) removes all the  
spaces.  
 
It looks terrifying, but it's faster than a Rexx loop.  If you're  
concerned about readability, the following is the same code but  
unrolled, and is also faster than a Rexx loop:  
 
    st = 'abc123def456ghi'  
    /* Generate string of unwanted characters. */  
    st1 = xrange('00'x,'2f'x) || xrange('3a'x,'ff'x)  
    /* Replace unwanted characters with spaces. */  
    st2 = translate( st, '', st1)  
    /* Remove all spaces. */  
    say space( st2, 0)  
 
- Perfesser Pete  
 
=====================================================  
 
To unsubscribe from this list, send an email message  
to "steward@scoug.com". In the body of the message,  
put the command "unsubscribe scoug-programming".  
 
For problems, contact the list owner at  
"rollin@scoug.com".  
 
=====================================================  
 
  
<< Previous Message << 
Return to [ 17 | 
September | 
1998 ] 
  
  
The Southern California OS/2 User Group
 P.O. Box 26904
 Santa Ana, CA  92799-6904, USA
Copyright 2001 the Southern California OS/2 User Group.  ALL RIGHTS 
RESERVED. 
 
SCOUG, Warp Expo West, and Warpfest are trademarks of the Southern California OS/2 User Group.
OS/2, Workplace Shell, and IBM are registered trademarks of International 
Business Machines Corporation.
All other trademarks remain the property of their respective owners.
 
 |