Program using String & Unstring

This is a Mainframe COBOL forum - you can post your queries on Mainframe COBOL, VS COBOL II, COBOL/370 , Enterprise COBOL

Moderators: dbzTHEdinosauer, Moderator Group

Post Reply
venky
Active Member
Posts: 50
Joined: Sat May 12, 2012 10:13 pm

Program using String & Unstring

Post by venky » Mon Sep 03, 2012 3:55 am

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.

User avatar
dbzTHEdinosauer
Moderator
Posts: 981
Joined: Mon Oct 02, 2006 8:31 pm

Post by dbzTHEdinosauer » Mon Sep 03, 2012 6:24 am

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".
Dick Brenholtz
JCL, SQL and code in programs have an irritating habit of doing what you say,
not what you meant.

William Collins
Active Member
Posts: 732
Joined: Thu May 24, 2012 4:07 am

Post by William Collins » Mon Sep 03, 2012 7:54 am

Did you look at the manual Venky?

You haven't supplied enough information to be certain:

Code: Select all

A,BC
A,,BC
A,B,C
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.

Code: Select all

This field intentionally left blank [that means just all blanks, not the text]
SACHOUT
Be clear about exactly what it says in the manual

DikDude
Moderator
Posts: 1001
Joined: Fri Jul 22, 2011 8:39 am
Location: usa

Post by DikDude » Mon Sep 03, 2012 8:04 am

One way might tbe to UNSTRING into 3 fields not 2. If the second field is blank, pre-pend a ',' infront of the firstname when STRINGing the new value.
Have a good one

William Collins
Active Member
Posts: 732
Joined: Thu May 24, 2012 4:07 am

Post by William Collins » Tue Sep 04, 2012 3:31 am

Here is your code with the additional tests I suggested. Doesn't work so well.

Code: Select all

ORIGINAL                                    
>TENDULKAR,SACHIN    <>SACHIN TENDULKAR    <
>SACHOUT             <>SACHIN SACHOUT      <
>                    <>SACHIN              <
>TENDULKAR,,SACHIN   <> TENDULKAR          <
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.

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<
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.

DikDude
Moderator
Posts: 1001
Joined: Fri Jul 22, 2011 8:39 am
Location: usa

Post by DikDude » Tue Sep 04, 2012 7:54 am

Possibly, i am missing something . . .

I believe this particular "requirement" is somewhat straight forward.

Why will the rather simple method i mentioned before not do the job?

Just wonderin' :)
Have a good one

William Collins
Active Member
Posts: 732
Joined: Thu May 24, 2012 4:07 am

Post by William Collins » Tue Sep 04, 2012 11:33 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....

Post Reply

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



Other References
Mainframe Tools and others