View previous topic :: View next topic
|
Author |
Message |
vinu78
Active User
Joined: 02 Oct 2008 Posts: 179 Location: India
|
|
|
|
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 |
|
|
Pandora-Box
Global Moderator
Joined: 07 Sep 2006 Posts: 1592 Location: Andromeda Galaxy
|
|
|
|
What do you need when the variable contains
AB
A
ACb
does your variable contain low values ? |
|
Back to top |
|
|
vinu78
Active User
Joined: 02 Oct 2008 Posts: 179 Location: India
|
|
|
|
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 |
|
|
Pandora-Box
Global Moderator
Joined: 07 Sep 2006 Posts: 1592 Location: Andromeda Galaxy
|
|
|
|
Also will your variable contain
bbbABCDb
where b stands for spaces? |
|
Back to top |
|
|
vinu78
Active User
Joined: 02 Oct 2008 Posts: 179 Location: India
|
|
|
|
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 |
|
|
Pandora-Box
Global Moderator
Joined: 07 Sep 2006 Posts: 1592 Location: Andromeda Galaxy
|
|
|
|
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 |
|
|
vinu78
Active User
Joined: 02 Oct 2008 Posts: 179 Location: India
|
|
|
|
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 |
|
|
Pandora-Box
Global Moderator
Joined: 07 Sep 2006 Posts: 1592 Location: Andromeda Galaxy
|
|
|
|
Apologies
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 |
|
|
vinu78
Active User
Joined: 02 Oct 2008 Posts: 179 Location: India
|
|
|
|
Thanks Pandora.
This seems to work fine. |
|
Back to top |
|
|
Bill Woodger
Moderator Emeritus
Joined: 09 Mar 2011 Posts: 7309 Location: Inside the Matrix
|
|
|
|
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 |
|
|
vinu78
Active User
Joined: 02 Oct 2008 Posts: 179 Location: India
|
|
|
|
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 |
|
|
Pandora-Box
Global Moderator
Joined: 07 Sep 2006 Posts: 1592 Location: Andromeda Galaxy
|
|
|
|
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
do you need
as output
or
as output |
|
Back to top |
|
|
enrico-sorichetti
Superior Member
Joined: 14 Mar 2007 Posts: 10886 Location: italy
|
|
|
|
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 |
|
|
vinu78
Active User
Joined: 02 Oct 2008 Posts: 179 Location: India
|
|
|
|
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 |
|
|
Pandora-Box
Global Moderator
Joined: 07 Sep 2006 Posts: 1592 Location: Andromeda Galaxy
|
|
|
|
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
Please try to make best use of the better code |
|
Back to top |
|
|
vinu78
Active User
Joined: 02 Oct 2008 Posts: 179 Location: India
|
|
|
|
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 |
|
|
Bill Woodger
Moderator Emeritus
Joined: 09 Mar 2011 Posts: 7309 Location: Inside the Matrix
|
|
|
|
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 |
|
|
vinu78
Active User
Joined: 02 Oct 2008 Posts: 179 Location: India
|
|
|
|
Thanks Bill. Thatz great solution. |
|
Back to top |
|
|
|