I have a 6-byte alphanumeric field, need to get the numeric value from this field. But the values will come like 123 ABC, 123ABC,1ABCDE,1234AB... etc. I have used NUMVAL function in the program it's not working and saying "Invalid character O was found in column 4 in argument-1 for function NUNUMVAL-C in program at displacement X'0EF8' ".
Could you please give me any solution for this.
Thanks in Advance....
Joined: 20 Oct 2006 Posts: 6968 Location: porcelain throne
as long as the numerics are in front (left side)
and the alpha is in end the (right side)
try with INSPECT CONVERTING a thru z to spaces then try the NUMVAL.
alternatively, bump an index thru the 6 bytes, and compare for 0 thru 9 otherwise convert to spaces.
if you use reference modification
use 1:1, 2:1, 3:1 == literals
if you use a variable for the postiion, you will end up with code resembling the inspect.
if you have values like '124ab2' you what would you expect as a result?
Subject to the answer to dbz's last question, you can do it by only finding the first non-numeric. That would give you the length of the numeric part. Handles non-alphas in there, if that is a possibility, that way.
Use most common local method for handling parts of fields (occurs depending on, reference-modification, UNSTRING, indexes, subscripts, blind faith).
Since you have a simple numeric, you don't "need" NUMVAL. Doesn't matter much, but the NUMVAL in the linked example is 5+ times slower than the straight code. You'd still have to do "a lot" of them to notice, however :-)
Joined: 14 Jan 2008 Posts: 2504 Location: Atlanta, Georgia, USA
An alternative -
Define a PIC 9(17) field and initialize it to zeros.
Then define PIC 9(08) COMP fields, SUB1 and SUB2, initializing SUB1 to the LENGTH OF STRING and SUB2 to the LENGTH of the PIC 9(17) field.
Then, starting at the last byte of the STRING (SUB1), using reference modification in an in-line PERFORM, test for NUMERICS and move each numeric-byte from STRING to the current-byte of the PIC 9(17) field (then subtract 1 from SUB2). Regardless, always subtract 1 from SUB1.
When SUB1 is less than 1, then your done and the PIC 9(17) field will contain the numerics, right-justified, with high-order zeros.