View previous topic :: View next topic |
Author |
Message |
enge Beginner
Joined: 12 Oct 2004 Posts: 78 Topics: 39
|
Posted: Wed Aug 17, 2005 9:33 am Post subject: max value in a string |
|
|
hi all,
i have a string pic x(8 ) and i must get the max value numeric.
for example:
BYEBYE01 ---- > return value 1
how can i do? (cobol language)
thx in advance. |
|
Back to top |
|
|
kolusu Site Admin
Joined: 26 Nov 2002 Posts: 12376 Topics: 75 Location: San Jose
|
Posted: Wed Aug 17, 2005 9:57 am Post subject: |
|
|
Enge,
Try this
Code: |
01 W-STRING PIC X(08) VALUE 'BYEBYE01'.
01 T-STR-TABLE REDEFINES W-STRING.
05 T-STR PIC X(01) OCCURS 8 TIMES.
01 W-SUB1 PIC S9(4) COMP.
01 W-SUB2 PIC S9(4) COMP.
01 W-TMP-STR PIC X(01).
PROCEDURE DIVISION.
PERFORM VARYING W-SUB1 FROM 1 BY 1 UNTIL W-SUB1 >= 8
PERFORM VARYING W-SUB2 FROM 8 BY -1
UNTIL W-SUB2 <= W-SUB1
EVALUATE TRUE
WHEN T-STR (W-SUB2) < T-STR (W-SUB2 - 1)
MOVE T-STR (W-SUB2) TO W-TMP-STR
MOVE T-STR (W-SUB2 - 1) TO T-STR (W-SUB2)
MOVE W-TMP-STR TO T-STR (W-SUB2 - 1)
WHEN T-STR (W-SUB2) > T-STR (W-SUB2 - 1)
CONTINUE
END-EVALUATE
END-PERFORM
END-PERFORM
DISPLAY 'The Max char is : ' T-STR(8)
|
Hope this helps...
Cheers
kolusu _________________ Kolusu
www.linkedin.com/in/kolusu |
|
Back to top |
|
|
bonnie_mathew Beginner
Joined: 05 Aug 2005 Posts: 8 Topics: 0
|
Posted: Thu Aug 18, 2005 4:04 am Post subject: |
|
|
Kolusu,
Correct me if I am wrong, but I think the following logic can do the job.
Code: |
MOVE T-STR(1) TO WS-TMP-STR
PERFORM
VARYING W-SUB1 FROM 1 BY 1 UNTIL W-SUB1 >= 8
IF T-STR (W-SUB1) > WS-TMP-STR
MOVE T-STR (W-SUB1) TO WS-TMP-STR
END-IF
END-PERFORM
|
|
|
Back to top |
|
|
kolusu Site Admin
Joined: 26 Nov 2002 Posts: 12376 Topics: 75 Location: San Jose
|
Posted: Thu Aug 18, 2005 7:04 am Post subject: |
|
|
bonnie_mathew,
Your code needs a sligt modification to work. Your perform statement should be performed until w-sub1 is greater than 8, so that you won't miss the last occurance.
Code: |
PERFORM VARYING W-SUB1 FROM 1 BY 1 UNTIL W-SUB1 > 8
|
My code will sort the entire table. I am aware that it is a overkill just to find out the max value. But if he wants to find the max and min values with the string, the above posted code does it all.
Thanks
Kolusu _________________ Kolusu
www.linkedin.com/in/kolusu |
|
Back to top |
|
|
|
|