Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Usergroups Profile Log in to check your private messages Log in
 

 

How to get last two char from a string?

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> COBOL Programming
View previous topic :: :: View next topic  
Author Message
Pons

New User


Joined: 25 May 2007
Posts: 61
Location: Coimbatore

PostPosted: Thu Mar 13, 2008 10:47 pm    Post subject: How to get last two char from a string?
Reply with quote

Hi,

How can i get the last two character of my variable using inspect or anything else.

Ex: My variable is
01 WS-ADDRESS PIC X(30).

Assume the valuse is like 'NEW YORK NY '.
There is no need only one spaces between two variables.
It may like 'NEW YORK NY '.

So i just want only last two characer NY alone...
How is it possible
Back to top
View user's profile Send private message

Craq Giegerich

Senior Member


Joined: 19 May 2007
Posts: 1512
Location: Virginia, USA

PostPosted: Thu Mar 13, 2008 11:18 pm    Post subject:
Reply with quote

Code:
01  ws-state      pic xx.
01  x1               pic s9(4) comp.
01 WS-ADDRESS PIC X(30).


PERFORM VARYING X1 FROM 29 BY -1 UNTIL X1 <= 0
   IF WS-ADDRESS (X1 : 1) > ' ' AND WS-ADDRESS (X1 + 1 : 1) > ' '
        MOVE WS-ADDRESS (X1 : 2) TO WS-STATE
        MOVE 0 TO X1
   END-IF
END-PERFORM.
Back to top
View user's profile Send private message
Gnanas N

Active Member


Joined: 06 Sep 2007
Posts: 788
Location: Chennai, India

PostPosted: Fri Mar 14, 2008 5:07 pm    Post subject:
Reply with quote

Code:
.
.
.
 
01 WS-STATE      PIC XX.                           
01 X1            PIC S9(4) COMP.                   
01 WS-ADDRESS    PIC X(30) VALUE 'NEW YORK NY'.     
.
.
.

INSPECT FUNCTION REVERSE(WS-ADDRESS)               
        TALLYING X1 FOR LEADING SPACE.             
COMPUTE X1 = 30 - 1 - X1.                         
MOVE WS-ADDRESS(X1:2) TO WS-STATE.                         
.
.
.

Is this okay?
Back to top
View user's profile Send private message
Pons

New User


Joined: 25 May 2007
Posts: 61
Location: Coimbatore

PostPosted: Fri Mar 14, 2008 5:13 pm    Post subject: Reply to: How to get last two char from a string?
Reply with quote

Thanks Giegerich and gnanas,

I have the same logic which you had Giegerich. But it looks like some what hard to me. Thats what i try to find some other logic. I try with Gnanas logic. Thanks again...
Back to top
View user's profile Send private message
Craq Giegerich

Senior Member


Joined: 19 May 2007
Posts: 1512
Location: Virginia, USA

PostPosted: Fri Mar 14, 2008 5:15 pm    Post subject:
Reply with quote

There are no leading spaces in 'NEW YORK NY '!
Back to top
View user's profile Send private message
Gnanas N

Active Member


Joined: 06 Sep 2007
Posts: 788
Location: Chennai, India

PostPosted: Fri Mar 14, 2008 5:25 pm    Post subject:
Reply with quote

Code:
INSPECT FUNCTION REVERSE(WS-ADDRESS)               
        TALLYING X1 FOR LEADING SPACE.


I have reversed it!!!
Back to top
View user's profile Send private message
mmwife

Super Moderator


Joined: 30 May 2003
Posts: 1592

PostPosted: Sat Mar 15, 2008 7:24 pm    Post subject:
Reply with quote

Hi Gnanas,

You might want to save yourself some future maint by coding:
Code:


COMPUTE X1 = LENGTH OF WS-STATE - (X1 + 1)
Back to top
View user's profile Send private message
Gnanas N

Active Member


Joined: 06 Sep 2007
Posts: 788
Location: Chennai, India

PostPosted: Tue Mar 18, 2008 6:12 pm    Post subject:
Reply with quote

Thanks, Jack.
Back to top
View user's profile Send private message
ranga_subham

New User


Joined: 01 Jul 2005
Posts: 51

PostPosted: Fri Mar 21, 2008 4:47 pm    Post subject: Why is this so complicated when could be achived easily.
Reply with quote

Hi,

Why can't we try the following?

Code:

WORKING-STORAGE SECTION.
01 WS-VAR  PIC X(20) VALUE 'NEW YORK NY'.
01 WS-VA1  PIC X(20) JUST RIGHT.         
PROCEDURE DIVISION.                 
MAIN-PARA.                           
    DISPLAY '*** PROGRAM STARTS ***'.
    DISPLAY WS-VAR.
    MOVE SPACES TO WS-VA1.   
    MOVE WS-VAR TO WS-VA1.   
    DISPLAY WS-VA1.         
    DISPLAY WS-VA1(18:2).   
    DISPLAY '*** PROGRAM ENDS ***'.
    STOP RUN.                       


Please suggest. icon_rolleyes.gif

Thanks.
Back to top
View user's profile Send private message
Marso

REXX Moderator


Joined: 13 Mar 2006
Posts: 1228
Location: Israel

PostPosted: Sun Mar 23, 2008 7:34 pm    Post subject:
Reply with quote

Ranga,
because the "JUST RIGHT" works only if you MOVE fields with different length (or if you're using UNSTRING):

Code:
WORKING-STORAGE SECTION.
01 WS-VAR  PIC X(20) VALUE 'NEW YORK NY'.
01 WS-VA1  PIC X(50) JUST RIGHT.         
PROCEDURE DIVISION.                 
MAIN-PARA.                           
    DISPLAY '*** PROGRAM STARTS ***'.
    DISPLAY WS-VAR.
    MOVE SPACES TO WS-VA1.   
    MOVE WS-VAR TO WS-VA1.   
    DISPLAY WS-VA1.         
    DISPLAY WS-VA1(18:2).   
    DISPLAY '*** PROGRAM ENDS ***'.
    STOP RUN.   

I have changed the length of WS-VA1 in your example. In this case, the 20 chars of WS-VAR will be moved to the 20 rightmost characters of WS-VA1 and it will be padded with 30 spaces on the left.
But within the 20 characters, nothing will be shifted. Which means you will now have spaces to the left AND to the right of your string...
Back to top
View user's profile Send private message
ranga_subham

New User


Joined: 01 Jul 2005
Posts: 51

PostPosted: Mon Mar 24, 2008 5:27 pm    Post subject:
Reply with quote

Thanks Marso.........Now, I got my basics right........... icon_biggrin.gif
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> COBOL Programming All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts Fileaid Update Replace with null string descann Compuware & Other Tools 4 Mon May 15, 2017 3:00 pm
No new posts Replacing same string with different ... vickey_dw DFSORT/ICETOOL 6 Wed Feb 22, 2017 10:44 pm
No new posts Count Trailing Spaces in variable str... Virendra Shambharkar SYNCSORT 10 Thu Feb 02, 2017 12:23 pm
No new posts Comparing Decimal and CHAR columns rakesh17684 DB2 7 Thu Oct 20, 2016 2:33 am
This topic is locked: you cannot edit posts or make replies. Checking wether a string starts from ... sundaram.naveen CLIST & REXX 9 Thu Oct 06, 2016 11:56 am


Facebook
Back to Top
 
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us