IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

How to check for the last 2 chars of a field of varying data


IBM Mainframe Forums -> COBOL Programming
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
JnanaR

New User


Joined: 20 Jul 2009
Posts: 23
Location: Mumbai

PostPosted: Fri Jun 11, 2010 3:14 pm
Reply with quote

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
View user's profile Send private message
Phrzby Phil

Senior Member


Joined: 31 Oct 2006
Posts: 1042
Location: Richmond, Virginia

PostPosted: Fri Jun 11, 2010 5:08 pm
Reply with quote

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
View user's profile Send private message
Binop B

Active User


Joined: 18 Jun 2009
Posts: 407
Location: Nashville, TN

PostPosted: Fri Jun 11, 2010 7:16 pm
Reply with quote

Hi Jnana,

Hoping I understood your requirement .... icon_razz.gif

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
View user's profile Send private message
Bill O'Boyle

CICS Moderator


Joined: 14 Jan 2008
Posts: 2501
Location: Atlanta, Georgia, USA

PostPosted: Fri Jun 11, 2010 7:47 pm
Reply with quote

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
View user's profile Send private message
CICS Guy

Senior Member


Joined: 18 Jul 2007
Posts: 2146
Location: At my coffee table

PostPosted: Fri Jun 11, 2010 8:24 pm
Reply with quote

JnanaR wrote:
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.
"The LENGTH function returns an integer equal to the length of the argument"
"The LENGTH OF special register contains the number of bytes used by a data item."
Back to top
View user's profile Send private message
Binop B

Active User


Joined: 18 Jun 2009
Posts: 407
Location: Nashville, TN

PostPosted: Fri Jun 11, 2010 9:52 pm
Reply with quote

Hi CICS Guy,

Just a little curious ... icon_razz.gif

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
View user's profile Send private message
CICS Guy

Senior Member


Joined: 18 Jul 2007
Posts: 2146
Location: At my coffee table

PostPosted: Fri Jun 11, 2010 10:12 pm
Reply with quote

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
View user's profile Send private message
JnanaR

New User


Joined: 20 Jul 2009
Posts: 23
Location: Mumbai

PostPosted: Sat Jun 12, 2010 12:22 am
Reply with quote

Thanks for the response guys.I have done it ery close to Bill's logic.
Thanks Bill....
Back to top
View user's profile Send private message
CICS Guy

Senior Member


Joined: 18 Jul 2007
Posts: 2146
Location: At my coffee table

PostPosted: Sat Jun 12, 2010 12:44 am
Reply with quote

Code:
IF WS-GROUP(LENGTH of WS-GROUP - 1:2) = 'XY'
  DO whatever
ELSE
  DO something else
END-IF
Back to top
View user's profile Send private message
Bill O'Boyle

CICS Moderator


Joined: 14 Jan 2008
Posts: 2501
Location: Atlanta, Georgia, USA

PostPosted: Sat Jun 12, 2010 3:26 am
Reply with quote

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 icon_redface.gif

Bill
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> COBOL Programming

 


Similar Topics
Topic Forum Replies
No new posts How to save SYSLOG as text data via P... All Other Mainframe Topics 2
No new posts Replacing 'YYMMDD' with date, varying... SYNCSORT 3
No new posts Store the data for fixed length COBOL Programming 1
No new posts Data set Rec-Cnt and Byte-Cnt Testing & Performance 2
No new posts Replace Multiple Field values to Othe... DFSORT/ICETOOL 12
Search our Forums:

Back to Top