View previous topic :: View next topic
|
Author |
Message |
JnanaR
New User
Joined: 20 Jul 2009 Posts: 23 Location: Mumbai
|
|
|
|
I have a field called WS-GROUP PIC X(15)
The length of the contents of the field can vary.
I need to put a check that if the last 2 chars of the value is 'XY'
I execute some code else I execute some other code.
The only thing that comes to my mind is that first I have to calculate the length of the contents of the field and then I can use reference modification for the check.But I am not sure what logic to use to calculate
the length of the data.Length function will always give 15 for the field which is not what I want.
Or I can read backwards and take the first 2 chars.Again I am not sure what logic to be used to read backwards.
Can anyone please give the logic?Thanks in advance... |
|
Back to top |
|
|
Phrzby Phil
Senior Member
Joined: 31 Oct 2006 Posts: 1042 Location: Richmond, Virginia
|
|
|
|
I presume you mean that the length of the non-blank contents can vary, as the length of the contents is always 15. Blank (x'40') is a character, too.
PERFORM VARYING a numeric variable from 15 by -1 UNTIL you have your two non-blanks or variable < 1, in which case it's all blank.
Suppose you have 'ABC D '. Do you want the blank-D?
If you have just one non-blank, then 14 blanks, do you want that? |
|
Back to top |
|
|
Binop B
Active User
Joined: 18 Jun 2009 Posts: 407 Location: Nashville, TN
|
|
|
|
Hi Jnana,
Hoping I understood your requirement ....
Use this link as a base for your requirement
Step 1 : Use INSPECT to get the count of trailing spaces
Step 2 : Use the count along with the reference modification to do the comparison ... |
|
Back to top |
|
|
Bill O'Boyle
CICS Moderator
Joined: 14 Jan 2008 Posts: 2501 Location: Atlanta, Georgia, USA
|
|
|
|
Another way, which won't expand into calling any COBOL run-time routine(s) -
Code: |
03 WS-GROUP PIC X(15).
03 WS-SUB PIC S9(08) BINARY.
03 WS-POS PIC S9(08) BINARY.
*
MOVE SPACES TO WS-GROUP.
MOVE 'XY' TO WS-GROUP (10:2).
MOVE LENGTH OF WS-GROUP TO WS-SUB.
MOVE ZERO TO WS-POS.
*
PERFORM UNTIL WS-SUB < 1
IF WS-GROUP (WS-SUB:1) = 'X'
IF WS-SUB < LENGTH OF WS-GROUP
IF WS-GROUP (WS-SUB + 1:1) = 'Y'
MOVE WS-SUB TO WS-POS
MOVE 1 TO WS-SUB
END-IF
END-IF
END-IF
ADD -1 TO WS-SUB
END-PERFORM.
*
IF WS-POS NOT = ZERO
PERFORM CHARACTERS-FOUND
ELSE
PERFORM CHARACTERS-NOT-FOUND
END-IF.
|
After completing the PERFORM, WS-POS will contain the position in WS-GROUP where 'XY' can be found, traversing backwards. In this example, the position will be 10.
The example code only recognizes the last occurrence in WS-GROUP of an 'X' followed immediately by a 'Y'.
Bill |
|
Back to top |
|
|
CICS Guy
Senior Member
Joined: 18 Jul 2007 Posts: 2146 Location: At my coffee table
|
|
Back to top |
|
|
Binop B
Active User
Joined: 18 Jun 2009 Posts: 407 Location: Nashville, TN
|
|
|
|
Hi CICS Guy,
Just a little curious ...
A snapshot from the link u sent ...
Quote: |
The intrinsic function LENGTH can also be used to obtain the length of a data item. For data items of usage NATIONAL , the length returned by the LENGTH function is the number of national character positions, rather than bytes; thus the LENGTH OF special register and the LENGTH intrinsic function have different results for data items of usage NATIONAL . For all other data items, the result is the same |
Will this work for OP's requirement ? |
|
Back to top |
|
|
CICS Guy
Senior Member
Joined: 18 Jul 2007 Posts: 2146 Location: At my coffee table
|
|
|
|
Without a compiler handy....
Since the "type of the function is integer" and reference modification requires a "positive nonzero integer" for the "leftmost-character-position", I thought it might. |
|
Back to top |
|
|
JnanaR
New User
Joined: 20 Jul 2009 Posts: 23 Location: Mumbai
|
|
|
|
Thanks for the response guys.I have done it ery close to Bill's logic.
Thanks Bill.... |
|
Back to top |
|
|
CICS Guy
Senior Member
Joined: 18 Jul 2007 Posts: 2146 Location: At my coffee table
|
|
|
|
Code: |
IF WS-GROUP(LENGTH of WS-GROUP - 1:2) = 'XY'
DO whatever
ELSE
DO something else
END-IF |
|
|
Back to top |
|
|
Bill O'Boyle
CICS Moderator
Joined: 14 Jan 2008 Posts: 2501 Location: Atlanta, Georgia, USA
|
|
|
|
Oops, just realized that populating WS-POS needs to be a calculation, not a move, because I'm going backwards (not forwards) and it should be -
Code: |
COMPUTE WS-POS = (LENGTH OF WS-GROUP - WS-SUB)
|
(15 - 5 = 10)
Slowly removing egg from face
Bill |
|
Back to top |
|
|
|