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

To get the last 4 non blank characters


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

Active User


Joined: 02 Oct 2008
Posts: 179
Location: India

PostPosted: Thu Dec 06, 2012 9:02 am
Reply with quote

Hi,

I have a field of pic X(8).

01 TIER-ID PIC X(8).

My reqt is to get the last 4 non blank characters and store it in a different field.

TIER-ID field values
GBZ123
ABCD
ZEDACCD
ABCDE

Output
Z123
ABCD
ACCD
BCDE

Can you please help ?

Thanks
Vinu
Back to top
View user's profile Send private message
Pandora-Box

Global Moderator


Joined: 07 Sep 2006
Posts: 1592
Location: Andromeda Galaxy

PostPosted: Thu Dec 06, 2012 9:22 am
Reply with quote

What do you need when the variable contains

AB
A
ACb

does your variable contain low values ?
Back to top
View user's profile Send private message
vinu78

Active User


Joined: 02 Oct 2008
Posts: 179
Location: India

PostPosted: Thu Dec 06, 2012 9:29 am
Reply with quote

We are making sure that the variable will always contain minimum 4 byte value or more (till 8 bytes) and will not contain Low values.

Thanks
Vinu
Back to top
View user's profile Send private message
Pandora-Box

Global Moderator


Joined: 07 Sep 2006
Posts: 1592
Location: Andromeda Galaxy

PostPosted: Thu Dec 06, 2012 9:36 am
Reply with quote

Also will your variable contain

bbbABCDb

where b stands for spaces?
Back to top
View user's profile Send private message
vinu78

Active User


Joined: 02 Oct 2008
Posts: 179
Location: India

PostPosted: Thu Dec 06, 2012 9:39 am
Reply with quote

The variable can contain spaces at the end not at the beginning.
The variable also will not contain spaces in between.

Thanks
Vinu
Back to top
View user's profile Send private message
Pandora-Box

Global Moderator


Joined: 07 Sep 2006
Posts: 1592
Location: Andromeda Galaxy

PostPosted: Thu Dec 06, 2012 9:47 am
Reply with quote

What you could try is

Code:
INSPECT TALLYING for SPACES

Get the count in WSCOUNT-1

Find the length of the variable in WSLENGTH-1

WS-START = (WSLENGTH-1 - WSCOUNT-1) - WSCOUNT-1+1

MOVE TIERID(WS-START:4) TO


If you are very sure the length doesnt change you could have value 8 in WSLENGTH-1

should work
Back to top
View user's profile Send private message
vinu78

Active User


Joined: 02 Oct 2008
Posts: 179
Location: India

PostPosted: Thu Dec 06, 2012 9:57 am
Reply with quote

Thanks for the code.
Just to make sure i am understanding it correctly.

Suppose if the pic x(8) variable value is ABCDE, then as per your logic,

WS-COUNT will contain 3 (3 spaces at end)
WS-LENGTH1 will contain 5
WS-START = (5-3) - 3 + 1 = 0

TIERID (0:4) ==>

Please let me know the logic behind WS-START

Thanks
Vinu
Back to top
View user's profile Send private message
Pandora-Box

Global Moderator


Joined: 07 Sep 2006
Posts: 1592
Location: Andromeda Galaxy

PostPosted: Thu Dec 06, 2012 10:20 am
Reply with quote

Apologies icon_redface.gif

I was wrong it should have been

Code:
INSPECT TALLYING for SPACES

Get the count in WSCOUNT-1

Find the length of the variable in WSLENGTH-1

WS-START = (WSLENGTH-1 - 4) - WSCOUNT-1+1

MOVE TIERID(WS-START:4) TO



Suppose if the pic x(8) variable value is ABCDE, then as per my logic,

WS-COUNT will contain 3 (3 spaces at end)
WS-LENGTH1 will contain 8
WS-START = (8-4) - 3 + 1 = 2


Suppose if the pic x(8) variable value is ABCDEFGH

WS-COUNT will contain 0
WS-LENGTH1 will contain 8
WS-START = (8-4) - 0 + 1 = 5
Back to top
View user's profile Send private message
vinu78

Active User


Joined: 02 Oct 2008
Posts: 179
Location: India

PostPosted: Thu Dec 06, 2012 10:47 am
Reply with quote

Thanks Pandora.
This seems to work fine.
Back to top
View user's profile Send private message
Bill Woodger

Moderator Emeritus


Joined: 09 Mar 2011
Posts: 7309
Location: Inside the Matrix

PostPosted: Thu Dec 06, 2012 1:37 pm
Reply with quote

vinu78,

You didn't answer all Pandora-Box's questions.

Lots of ways to do it. Make sure you test what you have fully, if you're going with that.

Code:
       IDENTIFICATION DIVISION.
       PROGRAM-ID. STUB20.
                                                                     
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  W-WHEN-COMPILED                      PIC X(8)BX(8).
       01  W-FIELD-TO-GET-LAST-4-OF-8           PIC X(8).
       01  W-LAST-4-OF-8                        PIC X(4)
                                                  JUSTIFIED RIGHT.
       PROCEDURE DIVISION.
                                                                     
           MOVE WHEN-COMPILED           TO W-WHEN-COMPILED
           DISPLAY "STUB20 " W-WHEN-COMPILED
           MOVE "ABCDE"                 TO W-FIELD-TO-GET-LAST-4-OF-8
           PERFORM                      10-DO-BUSINESS
           MOVE "ABCDEFGH"              TO W-FIELD-TO-GET-LAST-4-OF-8
           PERFORM                      10-DO-BUSINESS
           MOVE "AB      "              TO W-FIELD-TO-GET-LAST-4-OF-8
           PERFORM                      10-DO-BUSINESS
           MOVE SPACE                   TO W-FIELD-TO-GET-LAST-4-OF-8
           PERFORM                      10-DO-BUSINESS
                                                                     
           GOBACK
           .
       10-DO-BUSINESS.
           UNSTRING W-FIELD-TO-GET-LAST-4-OF-8
             DELIMITED BY SPACE
             INTO W-LAST-4-OF-8
           DISPLAY
                   ">"
                   W-FIELD-TO-GET-LAST-4-OF-8
                   "<"
           DISPLAY
                   ">"
                   W-LAST-4-OF-8
                   "<"
           .


Output is:

Code:
>ABCDE   <
>BCDE<   
>ABCDEFGH<
>EFGH<   
>AB      <
>  AB<   
>        <
>    <   


EDIT: For my own use, I can never resist the "Goldilocks Definition" of "JUST RIGHT", but for people unaware, it is best to write it in full, "JUSTIFIED RIGHT", so I have changed it here.
Back to top
View user's profile Send private message
vinu78

Active User


Joined: 02 Oct 2008
Posts: 179
Location: India

PostPosted: Thu Dec 06, 2012 8:03 pm
Reply with quote

Thanks Bill for the advice.
Your method also seems to be good.
However if the user id came as 3 digit (earlier I mentioned as it won't come but just curious to know the approach if it comes as 3 byte), in your approach, the user id will be extracted as - Eg: 'AB ' is extracted as ' AB' instead I need it as 'AB '.
So what changes we need to make in UNSTRING in that situation in your approach.


Pandora Box - I think you may have some other approach. The current approach will not handle user id less than 4 digits. So just curious to know whether we just need to put below mentioned check
Code:
IF WS-START < 0
    Move TIERID to output variable
ELSE
    Your approach mentioned above
END-IF.
Back to top
View user's profile Send private message
Pandora-Box

Global Moderator


Joined: 07 Sep 2006
Posts: 1592
Location: Andromeda Galaxy

PostPosted: Thu Dec 06, 2012 8:52 pm
Reply with quote

Quote:
We are making sure that the variable will always contain minimum 4 byte value or more (till 8 bytes) and will not contain Low values.


This is what you quoted when I asked if the value contains more spaces

Also when you have the best option why again revisit what is needed?

Also

when your variable has

Code:
AB       


do you need

Code:
AB 
as output

or
Code:
   AB


as output
Back to top
View user's profile Send private message
enrico-sorichetti

Superior Member


Joined: 14 Mar 2007
Posts: 10888
Location: italy

PostPosted: Thu Dec 06, 2012 8:52 pm
Reply with quote

Quote:
However if the user id came as 3 digit (earlier I mentioned as it won't come but just curious to know the approach if it comes as 3 byte), in your approach, the user id will be extracted as - Eg: 'AB ' is extracted as ' AB' instead I need it as 'AB '.


it would be nice if You posted all the requirement from the beginning.
You have no reason at all to complain about the solution posted
You never told what to do in case of <shorter> string ...
ALIGN-JUSTIFY LEFT or RIGHT ???
Back to top
View user's profile Send private message
vinu78

Active User


Joined: 02 Oct 2008
Posts: 179
Location: India

PostPosted: Thu Dec 06, 2012 10:13 pm
Reply with quote

I am sorry Pandora.
My reqt is working fine with your good solution.
Just curious to know what should be the approach if it contains 3 byte TIERID. The output TIERID should be left aligned.

Thanks
Vinu
Back to top
View user's profile Send private message
Pandora-Box

Global Moderator


Joined: 07 Sep 2006
Posts: 1592
Location: Andromeda Galaxy

PostPosted: Thu Dec 06, 2012 10:23 pm
Reply with quote

My solutions works pathetic for variable containing value > 4 space

If your input is always left aligned if count is greater than 4 you could move (1:4) to output

My solution was good until Bill gave the solution icon_smile.gif

Please try to make best use of the better code
Back to top
View user's profile Send private message
vinu78

Active User


Joined: 02 Oct 2008
Posts: 179
Location: India

PostPosted: Thu Dec 06, 2012 10:34 pm
Reply with quote

Pandora - Your solution is good since it is generic. ie., it works with any value from 4byte to 8 byte even if the value is floating. ie., it starts from 2nd byte.

Bill's solution is simple and good and it works superb for TIERID value between 1 and 8 bytes. However if the value is floating, this solution can have left alighment issues. ie., if 3 digit 'ABC ' comes as input , the output will be
' ABC' since it is right justified

So for me both solutions looks good.

~Vinu~
Back to top
View user's profile Send private message
Bill Woodger

Moderator Emeritus


Joined: 09 Mar 2011
Posts: 7309
Location: Inside the Matrix

PostPosted: Thu Dec 06, 2012 11:06 pm
Reply with quote

Remember, there are other ways.

You can use REDEFINEs and do the whole thing.

You can combine the concept of the REDEFINEs method with the UNSTRING.

Code:
01  W-FIELD-TO-GET-LAST-4-OF-8 PIC X(8).
01  FILLER REDEFINES W-FIELD-TO-GET-LAST-4-OF-8.
    05  W-FIELD-TO-GET-FIRST-4-OF-8 PIC X(4).
    05  FILLER PIC X.
       88  W-BYTE-IS-SPACE-SO-LEFT-ALIGN VALUE SPACE.
    05  FILLER PIC XXX.


Code:
IF W-BYTE-IS-SPACE-SO-LEFT-ALIGN
    MOVE W-FIELD-TO-GET-FIRST-4-OF-8 TO wherever
ELSE
    UNSTRING as previously, to wherever
Back to top
View user's profile Send private message
vinu78

Active User


Joined: 02 Oct 2008
Posts: 179
Location: India

PostPosted: Thu Dec 06, 2012 11:15 pm
Reply with quote

Thanks Bill. Thatz great solution.
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 Substring number between 2 characters... DFSORT/ICETOOL 2
No new posts Reading dataset in Python - New Line ... All Other Mainframe Topics 22
No new posts Merge files with a key and insert a b... DFSORT/ICETOOL 6
No new posts Count the number of characters in a f... CA Products 1
No new posts File transfer from host with filler f... TSO/ISPF 15
Search our Forums:

Back to Top