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

How to get last two char from a string?


IBM Mainframe Forums -> COBOL Programming
Post new topic   Reply to topic
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
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
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: 792
Location: Chennai, India

PostPosted: Fri Mar 14, 2008 5:07 pm
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
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
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: 792
Location: Chennai, India

PostPosted: Fri Mar 14, 2008 5:25 pm
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
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: 792
Location: Chennai, India

PostPosted: Tue Mar 18, 2008 6:12 pm
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
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: 1353
Location: Israel

PostPosted: Sun Mar 23, 2008 7:34 pm
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
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 View Bookmarks
All times are GMT + 6 Hours
Forum Index -> COBOL Programming

 


Similar Topics
Topic Forum Replies
No new posts Replace each space in cobol string wi... COBOL Programming 3
No new posts PARSE Syntax for not fix length word ... JCL & VSAM 7
No new posts SFTP Issue - destination file record ... All Other Mainframe Topics 2
No new posts Sortjoin and Search for a String and ... DFSORT/ICETOOL 1
No new posts Exclude rows with > than x occurre... DFSORT/ICETOOL 6
Search our Forums:

Back to Top