PDA

View Full Version : Converting Character Data to Numeric Data

Guest.Visitor
05-03-2002, 10:43 AM
Attached is a small RPG program with a procedure that extracts the digits from a string. It should be fairly easy to convert if you're not yet on V5R1. BTW, for fun, I also wrote a function in Python that does the same thing: <code>def digs(s): return ''.join([x for x in s if x in string.digits])</code> Cheers! Hans

getdigs.rpg (http://www.mcpressonline.com/images/fbfiles/files/4e688ff8_getdigs.rpg)

Guest.Visitor
05-05-2002, 08:01 AM
In REXX it's, digits: return, space(translate(arg,' ',xrange('00'x,'/')||xrange(':','EF'x)),0); and somewhere I've seen a slicker REXX digits pgm, but cannot seem to find it. I've started several times to learn Python, but found a way to use rexx in CGI. bobh

Guest.Visitor
05-06-2002, 05:24 AM
I need to extract a 4 digit page number from a 9 character alpha field. The field may contain '[]' ',' roman numerals or other miscellaneous alpha characters not pertinent to the numeric data. Does anyone have an easy way to scan the field and extract only the numbers? Thank you, Erik J. Osberg

Guest.Visitor
05-06-2002, 05:24 AM
That's some pretty kludgy coding, Bob. I was going to comment that it wouldn't work on an EBCDIC machine, but then I realized how it works on either ASCII or EBCDIC - the proc returns characters in the range x30..x39 and in the range xF0..xFF. Pretty sneaky. For robustness, it would be better to tailor that proc to the environment, and return characters just in the specific range for the character set. On EBCDIC, use <code>xrange('00'x,'ef'x)||xrange('fa'x,'ff'x)</code>, and on ASCII use <code>xrange('00'x,'/')||xrange(':','ff'x)</code>. (To tell the difference, you could compare 'a' to 'A'. If less, you have an EBCDIC machine.) Regarding comparing REXX to Python, there are similarities and differences between the two languages. On the one hand, REXX is already available to iSeries programmers, and, like Python, the language is relatively easy to learn. On the other hand, Python comes with a large set of libraries that take full advantage of Python's object-oriented features. Object REXX has an object model similar to Python's, but does not seem to be used to the same extent as objects in Python.