Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

need to point to seq number of the letter of alphabet 3

Status
Not open for further replies.

noraman

Programmer
Dec 8, 2004
7
0
0
US
I need to be able to 'convert' letters of alphabet to their numeric seq number, for example 'A' is 1... Z is 26...
FUNCTION NUMVAL is for numeric values only, not alpha.
Does anyone know any other intrinsic functions or any other way to do it?

thank you.
noraman.
 
Right off the top of my tired mind, without having to use an ASCII value table (which would be the ideal solution), you can build an array (A-Z). Loop through it looking for a match. The subscript value is your numeric equivalent.
 
someone has sent me this, I'll share with group:

WORKING-STORAGE SECTION.
01 OrdPos PIC 99.
01 IntArray VALUE "1223037865".
02 Ielement OCCURS 5 TIMES PIC 99.

PROCEDURE DIVISION.
Begin.


MOVE FUNCTION ORD("A") TO OrdPos
DISPLAY "Ord pos of A is = " OrdPos

answer:
Ord pos of A is = 66

noraman
 
I think that's the ASCII value solution I spoke of. If your compiler has that function.

It looks like all you'll have to do is subtract 65 from the value you get back.
 
you are absolutely right!!!
I have tested this on the mainframe, and
the result is not 66, the result integer for 'A'
is 194 because 'A' is a decimal 193.

works fine!.

thanks for input.
 
But be aware that the letters A-Z are not contiguous in EBCDIC!! A=193, but Z=233, not 219.

Regards.

Glenn
 
Here is a nonintrinsic way to accomplish this in COBOL that is not vunerable to character set representation.
Code:
01  char-to-convert pic x.
01  ordinal-position pic 99.
01  redefines ordinal-position.
    02  ordinal-most-sig-digit pic X.
    02  ordinal-least-sig-digit pic X.
...
    move char-to-convert to ordinal-most-sig-digit
                            ordinal-least-sig-digit
    inspect ordinal-most-sig-digit
        converting "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
                to "00000000011111111112222222".
    inspect ordinal-least-sig-digit
        converting "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
                to "12345678901234567890123456".
    display "The ordinal position of ",
            char-to-convert, " is ",
            ordinal-position.

Tom Morrison
 
Code:
01 OrdChar    pic X(01).
01 OrdPos     pic 9(02).
01 ConstChars pic x(27) value "_ABCDEFGHIJKLMNOPQRSTUVWXYZ".

inspect ConstChars tallying OrdPos for characters before initial OrdChar

It can be done a little shorter? Or is this vendor-specific for MF?

HTH
TonHu
 
TonHu,

A small correction/optimization:
Code:
01 OrdChar    pic X(01).
01 OrdPos     pic 9(02).
01 ConstChars pic x(27) value "ABCDEFGHIJKLMNOPQRSTUVWXYZ".

move 1 to OrdPos.
inspect ConstChars tallying OrdPos for characters before initial OrdChar
You must initialize the tallying variable, so start it at 1 (rather than 0) and eliminate the confusing placeholding character.

I would say that this is indeed a better solution than my suggestion. (My solution could be used to obtain noncontiguous code points, but that was not the requirement.)

Tom Morrison
 
Hi Noraman,

If you're interested in efficiency, you might want to consider something like this:

It allows you to use the character in question as an index (subscript) into the table that contains the numeric equivalent of the alpha char. I've included the alpha, as well as the numeric value, in the table. You can drop the alpha if you like.

The advantage to this approach is that you only need a subscripted move to get the value you need or reference the value w/the subscript as in the DISPLAY stmt below.
Code:
01  TBL-VALS.
05  FILLER PIC  X(576)  VALUE ' '.
05  A-I    PIC  X(027)  VALUE
    'A01B02C03D04E05F06G07H08I09'.
05  FILLER PIC  X(021)  VALUE ' '.
05  J-R    PIC  X(027)  VALUE
    'J10K11L12M13N14O15P16Q17R18'.
05  FILLER PIC  X(024)  VALUE ' '.
05  S-Z    PIC  X(024)  VALUE
    'S19T20U21V22W23X24Y25Z26'.
05  FILLER PIC  X(066)  VALUE ' '.

01  A-Z-TBL REDEFINES TBL-VALS.
05  TBL-ENTRY OCCURS 255  TIMES.
    10  ALPHA-VAL    PIC  X.
    10  NUM-VAL      PIC  XX.

01  SUB  PIC S9(002) VALUE ZERO.
01  SUB1 REDEFINES   SUB.
    05  SUB-POS1 PIC  X(001). 
    05  SUB-POS2 PIC  X(001). 


(code)
MOVE your-char TO SUB-POS2
IF SUB = ZERO do error stuff
IF NUM-VAL(SUB) = SPACES do error stuff

DISPLAY 'ALPHA CHAR >' ALPHA-VAL(SUB) '< IS AT POS >' 
NUM-VAL(SUB) '< IN THE ALPHABET'

P.S. This was written w/o benefit of manuals so check the syntax and the table math.

Regards, Jack.

"A problem well stated is a problem half solved" -- Charles F. Kettering
 
Oops!! Line:
Code:
01  SUB  PIC S9(002) VALUE ZERO.
should be:
Code:
01  SUB  PIC S9(002) COMP VALUE ZERO.


Regards, Jack.

"A problem well stated is a problem half solved" -- Charles F. Kettering
 
k5tm,

Thanks for the corrections, I never put this through any test (except for compileability), so it came right from the back (dark side) of my mind ;-)

Thnx
TonHu
 
Tom Morrison's optimized/corrected implementation is by far the best for a generic environment and compiler. The only thing I would add is error and case detection, but these were not stated in the original problem.
 
thank you all for sharing ideas!
code is implemented!!![img
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top