View previous topic :: View next topic
|
Author |
Message |
David McCrina
New User
Joined: 11 Apr 2013 Posts: 6 Location: Atlanta, GA USA
|
|
|
|
Hi...I have searched the forum and could not find an answer to this, if it has already been addressed I apologize.
Here is what I would like to do:
A one character field can have a value of A-Z, 0-9. I want to use that value as an index to a table position (A-Z = positions 1-26, 0-9 = positions 27-36 in the table).
I have looked at maybe using the INSPECT CONVERTING, but that seems to be a one-to-one correspondence...you have to convert to the same length as the field you are inspecting.
I have also thought about the NUMVAL function, but does that only convert an alpha field containing numerics to a numeric field, or does it convert the value A to a numeric equivalent (like hex C1)?
Any ideas how a can make that translation from A-Z, 0-9 to 1-36?
Thanks! |
|
Back to top |
|
|
Robert Sample
Global Moderator
Joined: 06 Jun 2008 Posts: 8697 Location: Dubuque, Iowa, USA
|
|
|
|
Method 1: use EVALUATE on the 1-byte variable
Method 2: use IF statements on the 1-byte variable
Method 3: use FUNCTION ORD in either EVALUATE or IF statements (this needs multiple uses since A to Z are not sequential in the collating sequence) |
|
Back to top |
|
|
David McCrina
New User
Joined: 11 Apr 2013 Posts: 6 Location: Atlanta, GA USA
|
|
|
|
Thanks for the reply...I was hoping to avoid having to code 36 occurrences of either a WHEN condition or ELSE condition.
I was wondering if there was either a way to do it using a different COBOL verb or function, or a mathematical routine to make the translation.
I don't know if you could use the hex values (C1-C9, D1-D9, E2-E9, F0-F9) somehow.
I'll look at the ORD function. |
|
Back to top |
|
|
David McCrina
New User
Joined: 11 Apr 2013 Posts: 6 Location: Atlanta, GA USA
|
|
|
|
Quick question...can you do something like:
INSPECT FIELD CONVERTING X'C1C2...F9' to X'0102...24'? |
|
Back to top |
|
|
Akatsukami
Global Moderator
Joined: 03 Oct 2009 Posts: 1788 Location: Bloomington, IL
|
|
|
|
Is the performance of a PERFORM VARYING unacceptable? |
|
Back to top |
|
|
Bill O'Boyle
CICS Moderator
Joined: 14 Jan 2008 Posts: 2501 Location: Atlanta, Georgia, USA
|
|
|
|
David,
Welcome to the forum.
Binary-Fullwords are your friend.
How about this -
Code: |
03 WS-SUB PIC 9(08) BINARY.
03 WS-SUB-X REDEFINES WS-SUB PIC X(04).
03 WS-ARG PIC X(01).
MOVE ZERO TO WS-SUB.
MOVE 'A' TO WS-ARG.
INSPECT WS-ARG CONVERTING 'ABCDEFGHIJLKMNOPQRSTUVWXYZ0123456789'
TO X'0102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F2021222324'.
MOVE WS-ARG TO WS-SUB-X (LENGTH OF WS-SUB-X:).
|
At this point, WS-SUB (can be used as a table-subscript) will be in the range of decimal 01 thru 36, with values of 01-26 representing letters 'A' thru 'Z' and values of 27-36 representing numerics '0' thru '9'.
Because you're using literals, the underlying INSPECT CONVERTING generates an In-Line Assembler "TR" (Translate) instruction (under the covers), so it's very efficient. The compiler will also build the "FROM" and "TO" translate-tables in its Dynamic-Storage.
Always define WS-SUB as a fullword and you'll stay out of trouble with COBOL idiosyncrasies and how it deals with halfwords. It's only two-bytes.
HTH.... |
|
Back to top |
|
|
David McCrina
New User
Joined: 11 Apr 2013 Posts: 6 Location: Atlanta, GA USA
|
|
|
|
Akatsukami wrote: |
Is the performance of a PERFORM VARYING unacceptable? |
I am not sure how the PERFORM VARYING would help, can you give the example?
Thanks |
|
Back to top |
|
|
David McCrina
New User
Joined: 11 Apr 2013 Posts: 6 Location: Atlanta, GA USA
|
|
|
|
Bill...thanks, that looks like a good solution. I'll give it a shot! |
|
Back to top |
|
|
Bill Woodger
Moderator Emeritus
Joined: 09 Mar 2011 Posts: 7309 Location: Inside the Matrix
|
|
|
|
Table with 36 values, PERFORM VARYING to find equal for the one you want.
SEARCH. SEARCH ALL.
Various ways relying on the right-most half-byte and the entire byte in combination.
With the INSPECT, ensure that you cater for/have catered for processing a "character" that is not A-Z or 0-9. |
|
Back to top |
|
|
David McCrina
New User
Joined: 11 Apr 2013 Posts: 6 Location: Atlanta, GA USA
|
|
|
|
Thanks Bill...by the time the INSPECT will be done the value would have already been validated, so we're good there. |
|
Back to top |
|
|
|