Program using String & Unstring
Moderators: dbzTHEdinosauer, Moderator Group
Program using String & Unstring
Hi,
I have coded a program by using string & unstring. Please see below.
ID DIVISION.
PROGRAM-ID UNSTR.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 NAME PIC X(20) VALUE 'TENDULKAR,SACHIN'.
01 FNAME PIC X(20).
01 LNAME PIC X(20).
01 EXTNAME PIC X(20).
PROCEDURE DIVISION.
UNSTRING NAME DELIMITED BY ',' INTO LNAME, FNAME
END-UNSTRING.
STRING FNAME DELIMITED BY SPACE ' ' LNAME DELIMITED
BY SIZE INTO EXTNAME
END-STRING.
DISPLAY EXTNAME.
STOP RUN.
for the above program, I am getting the correct output see below
Input : TENDULKAR,SACHIN
Output : SACHIN TENDULKAR
but I have a requirement. Please see below
I have a input as : TENDULKAR,,SACHIN
now I need the Output as : ,SACHIN TENDULKAR
Note: It should delimit only first comma(,) from the input
Please look into this and provide me the solution. Thanks.
Regards,
Venky.
I have coded a program by using string & unstring. Please see below.
ID DIVISION.
PROGRAM-ID UNSTR.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 NAME PIC X(20) VALUE 'TENDULKAR,SACHIN'.
01 FNAME PIC X(20).
01 LNAME PIC X(20).
01 EXTNAME PIC X(20).
PROCEDURE DIVISION.
UNSTRING NAME DELIMITED BY ',' INTO LNAME, FNAME
END-UNSTRING.
STRING FNAME DELIMITED BY SPACE ' ' LNAME DELIMITED
BY SIZE INTO EXTNAME
END-STRING.
DISPLAY EXTNAME.
STOP RUN.
for the above program, I am getting the correct output see below
Input : TENDULKAR,SACHIN
Output : SACHIN TENDULKAR
but I have a requirement. Please see below
I have a input as : TENDULKAR,,SACHIN
now I need the Output as : ,SACHIN TENDULKAR
Note: It should delimit only first comma(,) from the input
Please look into this and provide me the solution. Thanks.
Regards,
Venky.
- dbzTHEdinosauer
- Moderator
- Posts: 981
- Joined: Mon Oct 02, 2006 8:31 pm
maybe change ,, to something else and use a different UNSTRING delimiter.
that's your solution.
and if you are going to say "provide me the solution"
then I am going to say, "provide me with your paycheck".
that's your solution.
and if you are going to say "provide me the solution"
then I am going to say, "provide me with your paycheck".
Dick Brenholtz
JCL, SQL and code in programs have an irritating habit of doing what you say,
not what you meant.
JCL, SQL and code in programs have an irritating habit of doing what you say,
not what you meant.
-
- Active Member
- Posts: 732
- Joined: Thu May 24, 2012 4:07 am
Did you look at the manual Venky?
You haven't supplied enough information to be certain:
If the third cannot exist, then DELIMITED BY ALL "," with DELIMITER IN fieldname. fieldname would need to be the length of the maximum number of repeating commas possibile.
Sub-define fieldname such that bytes two to end have a name.
Specify three fields in your STRING by including the sub-defined name in the middle.
If the third can exist, use UNSTRING with COUNT IN, but only specifying one destination field, the first.
Then, use a second UNSTRING with WITH POINTER but specifying no delimiter to get the second field, with a potential leading/embedded character the same value as your delimiter in the first UNSTRING now being of no significance.
Don't use TALLYING to adjust the value from the COUNT IN as you always know the value to adjust it by - although think about what to do if the field is already space, which you aren't doing at the moment.
Put these values as well into your program, at the same time, after your existing test data and run your original.
Be clear about exactly what it says in the manual
You haven't supplied enough information to be certain:
Code: Select all
A,BC
A,,BC
A,B,C
Sub-define fieldname such that bytes two to end have a name.
Specify three fields in your STRING by including the sub-defined name in the middle.
If the third can exist, use UNSTRING with COUNT IN, but only specifying one destination field, the first.
Then, use a second UNSTRING with WITH POINTER but specifying no delimiter to get the second field, with a potential leading/embedded character the same value as your delimiter in the first UNSTRING now being of no significance.
Don't use TALLYING to adjust the value from the COUNT IN as you always know the value to adjust it by - although think about what to do if the field is already space, which you aren't doing at the moment.
Put these values as well into your program, at the same time, after your existing test data and run your original.
Code: Select all
This field intentionally left blank [that means just all blanks, not the text]
SACHOUT
-
- Active Member
- Posts: 732
- Joined: Thu May 24, 2012 4:07 am
Here is your code with the additional tests I suggested. Doesn't work so well.
Here's my first suggestion. Turns out not to work. The DELIMITER IN is only to distinguish between multiple different delimiters, not to do that and show how many delimiter characters have been processed when ALL has been used. There doesn't seem a direct way to do that.
I do it a different way when the data is more complex than easily allows UNSTRING'/STRING to work with. This is just on the edge. It will work in all cases.
dbz's suggestion will work, as long as the chosen replacement for "," cannot appear naturally in the field.
INSPECT field REPLACING FIRST COMMA BY new-delimiter : UNSTRING using the new delimeter.
Usually when you are UNSTRINGing, you have text data, so no problem with finding something unused to make the delimiter. I think this is the neatest solution, as many will be unfamiliar with COUNT IN and WITH POINTER (not to be confused with the other sort of POINTERs, you see?) and don't know where/how to read the manual.
Code: Select all
ORIGINAL
>TENDULKAR,SACHIN <>SACHIN TENDULKAR <
>SACHOUT <>SACHIN SACHOUT <
> <>SACHIN <
>TENDULKAR,,SACHIN <> TENDULKAR <
Code: Select all
SUGGESTION 1
>, <
>TENDULKAR,,SACHIN <>SACHINTENDULKAR <
Code: Select all
Suggestion 2 works. It consists of two UNSTRINGs, the first delimited by a single "," the second undelimited and using the WITH POINTER with the value from COUNT IN adjusted for the delimiter.
SUGGESTION 2
>TENDULKAR,,SACHIN <>,,SACHIN TENDULKAR <
>TENDULKAR,,,SAC,H,IN<>,,,SAC,H,IN TENDULKA<
dbz's suggestion will work, as long as the chosen replacement for "," cannot appear naturally in the field.
INSPECT field REPLACING FIRST COMMA BY new-delimiter : UNSTRING using the new delimeter.
Usually when you are UNSTRINGing, you have text data, so no problem with finding something unused to make the delimiter. I think this is the neatest solution, as many will be unfamiliar with COUNT IN and WITH POINTER (not to be confused with the other sort of POINTERs, you see?) and don't know where/how to read the manual.
-
- Active Member
- Posts: 732
- Joined: Thu May 24, 2012 4:07 am
Yes, strictly sticking to what is shown and said that will work fine.
My concern is "what the heck is that extra comma anyway"? I suspect "data entry". In which case, if there is one, there can be two, or more, and there can be one, two, or more, in "random" locations.
Everrything, except my failed solution 1, works with one comma in the place shown, and even keeps all the data with one comma anywhere.
With two commas, two of the three "workers" keep going, and one falls by the wayside.
So the system-tester puts a second extra comma in, coder puts a fourth field....
By the time there are eight extra fields, TS (currently AWOL) is worried their program looks untidy , and the chosen field-name (VAR1-9) is approaching the limit....
My concern is "what the heck is that extra comma anyway"? I suspect "data entry". In which case, if there is one, there can be two, or more, and there can be one, two, or more, in "random" locations.
Everrything, except my failed solution 1, works with one comma in the place shown, and even keeps all the data with one comma anywhere.
With two commas, two of the three "workers" keep going, and one falls by the wayside.
So the system-tester puts a second extra comma in, coder puts a fourth field....
By the time there are eight extra fields, TS (currently AWOL) is worried their program looks untidy , and the chosen field-name (VAR1-9) is approaching the limit....
FREE TUTORIALS
Tutorials
Free tutorials from mainframegurukul
- JCL Tutorial
Covers all important JCL concepts. - Cobol Tutorial
This tutorials covers all Cobol Topics from STRING to COMP-3. - DB2 Tutorial
DB2 Tutorial focuses on DB2 COBOL Programming. - SORT Tutorial
This Tutorial covers all important aspects of DFSORT with examples - CICS Tutorial
This CICS tutorial covers CICS concepts and CICS Basics, CICS COBOL Programming.
Interview
Mainframe Interview questions
- Cobol Interview Questions
50+ Interview Questions - JCL Interview Questions
50+ Interview Questions - DB2 Interview Questions
100+ Interview Questions - CICS Interview Questions
70+ Interview Questions - VSAM Interview Questions
27 Interview Questions
Other References
Mainframe Tools and others
- XPEDITER Reference
Explains how we can debug a program - FILEAID Reference
Explains how to browse , edit and delete datasets - Change Man Reference
Quick Start tutorial on Changeman - Abend Reference
Important Abend codes explained - FaceBook Page
MainframeGurukul FaceBook Page - LinkedIn Page
MainframeGurkul Linkedin Page