Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Usergroups Profile Log in to check your private messages Log in
 

 

Need a help on Unstring
Goto page 1, 2  Next
 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> COBOL Programming
View previous topic :: :: View next topic  
Author Message
Rohit Umarjikar

Senior Member


Joined: 21 Sep 2010
Posts: 1609
Location: NY,USA

PostPosted: Thu Nov 29, 2012 2:33 am    Post subject: Need a help on Unstring
Reply with quote

xyz file:

Code:
11/21/2012,,3.125%,"$172,000.00",$0.00,"$172,000.00"


Here I am executing,

Code:
UNSTRING xyz-rec DELIMITED BY X'6B' into
ws-date1
ws-date2
ws-perctge
ws-amt1
ws-amt2
ws-amt3
END-UNSTRING


output I am getting is,

Code:
11/21/2012

3.125%
"$172
$0.00
"$172


But I need ,

Code:
11/21/2012

3.125%
"$172,000.00"
$0.00
"$172,000.00"




Can you please guide further to get this?
Back to top
View user's profile Send private message

Robert Sample

Global Moderator


Joined: 06 Jun 2008
Posts: 7904
Location: Bellevue, IA

PostPosted: Thu Nov 29, 2012 2:39 am    Post subject:
Reply with quote

UNSTRING won't help you here. Either get the source of the data to remove the commas inside the quotes, or use reference modification (or arrays) to examine each byte, one at a time, to determine how to process it.
Back to top
View user's profile Send private message
Akatsukami

Global Moderator


Joined: 03 Oct 2009
Posts: 1738
Location: Bloomington, IL

PostPosted: Thu Nov 29, 2012 2:39 am    Post subject:
Reply with quote

How you propose that the comma (X'6B') delimiting the fields be distinguished from the comma (X'6B') serving as a separator in the amounts?
Back to top
View user's profile Send private message
Rohit Umarjikar

Senior Member


Joined: 21 Sep 2010
Posts: 1609
Location: NY,USA

PostPosted: Thu Nov 29, 2012 2:47 am    Post subject:
Reply with quote

@Akatsukami:
That is the approach we decided, but now it is getting difficult to go with '6B'
Back to top
View user's profile Send private message
Rohit Umarjikar

Senior Member


Joined: 21 Sep 2010
Posts: 1609
Location: NY,USA

PostPosted: Thu Nov 29, 2012 2:51 am    Post subject:
Reply with quote

Robert: How reference modification will help here as the data may come and may not come like happened for ws-date2 filed in the above example
Back to top
View user's profile Send private message
Simone Salzmann

New User


Joined: 26 Nov 2012
Posts: 17
Location: Switzerland

PostPosted: Thu Nov 29, 2012 3:20 am    Post subject:
Reply with quote

If you can't have the delimiters changed at their origin they come into COBOL you could try this:

Before unstringing the data stream you could replace the commas outside the " " by some other character that isn't used anywhere in the datastream and unstring with the help of that character.

Try the COBOL handbooks for INSPECT (TALLYING) REPLACING...

Possibly you'll have to build a loop to get through the complete data stream, replacing the commas outside the " " and just jumping forward to the end of the " " part.

Could that be a solution for you?

It would be the easiest, though, to ask for different delimiters to be delivered.
Back to top
View user's profile Send private message
Akatsukami

Global Moderator


Joined: 03 Oct 2009
Posts: 1738
Location: Bloomington, IL

PostPosted: Thu Nov 29, 2012 3:22 am    Post subject:
Reply with quote

Although I am not a COBOL programmer (in the past couple of decades, anyway), the algorithm is as applicable to COBOL as it is to PL/I or Rexx.
Code:

j        = 0
mark     = 1
inquotes = FALSE

do i = 1 to length(record)
  if (char(i)=double_quote) then
    inquotes = ¬inquotes
 
  if ((char(i)=comma) & (¬inquotes)) then
  do
    j += 1
    receiving_field(j) = substr(record,mark,i-1)
    mark = i + 1
  end
end

(I assume that it's obvious what values comma and double_quote have.)
Back to top
View user's profile Send private message
enrico-sorichetti

Global Moderator


Joined: 14 Mar 2007
Posts: 10201
Location: italy

PostPosted: Thu Nov 29, 2012 3:24 am    Post subject: Reply to: Need a help on Unstring
Reply with quote

here is a POC in rexx


Code:
src = "11/21/2012,,3.125%,""$172,000.00"",$0.00,""$172,000.00"""
len = length(src)

str = 0
j   = 1
fld.= ""

do  i = 1 to len
    c = substr(src,i,1)
    if  c = "," then do
        if  str then ,
            fld.j = fld.j || c
        else ,
            j = j + 1
        iterate
    end
    fld.j = fld.j || c
    if  c = """" then ,
        str = \str
end

do  f = 1 to j
    say f fld.f
end


the result


Code:
1 11/21/2012
2
3 3.125%
4 "$172,000.00"
5 $0.00
6 "$172,000.00"
Back to top
View user's profile Send private message
Bill Woodger

DFSORT Moderator


Joined: 09 Mar 2011
Posts: 7223

PostPosted: Thu Nov 29, 2012 4:55 am    Post subject: Re: Need a help on Unstring
Reply with quote

Quote:
xyz file:

Code:
11/21/2012,,3.125%,"$172,000.00",$0.00,"$172,000.00"


Here I am executing,

Code:
UNSTRING xyz-rec DELIMITED BY X'6B' into
ws-date1
ws-date2
ws-perctge
ws-amt1
ws-amt2
ws-amt3
END-UNSTRING


output I am getting is,

Code:
11/21/2012

3.125%
"$172
$0.00
"$172




How do you get that output? I'd expect this:

Code:
11/21/2012

3.125%
"$172
000.00"
$0.00


Your Sort product can use PARSE with PAIR= to "protect" the embedded commas.

It shouldn't be difficult for the producer of the data to give you a file lacking the comma number-editing or using a different delimiter.

If you end up having to do it in Cobol with the file as then there is an OCCURS DEPENDING ON solution to go with those from Mr Akatsukami and enrico.
Back to top
View user's profile Send private message
saiprasadh

Active User


Joined: 20 Sep 2006
Posts: 154
Location: US

PostPosted: Thu Nov 29, 2012 5:07 am    Post subject:
Reply with quote

I used below COBOL code for unstring the data

Code:
       WORKING-STORAGE SECTION.                                         
       *                                                                 
        01 WS-TEST-DATA             PIC X(100).                         
        01 WS-TEMP-DATA             PIC X(100).                         
        01 WS-START-POSN            PIC S9(04) COMP.                     
        01 WS-SPACE-COUNT           PIC S9(04) COMP.                     
        01 WS-DATA-LEN              PIC S9(04) COMP.                     
        01 WS-PROCESS-CNT           PIC S9(04) COMP.                     
        01 WS-POSN                  PIC S9(04) COMP.                     
       *                                                                 
        LINKAGE SECTION.                                                 
       *                                                                 
        PROCEDURE DIVISION.                                             
       *                                                                 
        0000-MAINLINE.                                                   
       *                                                                 
            MOVE '11/21/2012,,3.125%,"$172,000.00",$0.00,"$172,000.00"' 
                                      TO WS-TEST-DATA                   
            MOVE ZERO                 TO WS-SPACE-COUNT 
            INSPECT FUNCTION REVERSE(WS-TEST-DATA)       
                    TALLYING WS-SPACE-COUNT             
            FOR LEADING SPACES                           
            COMPUTE WS-DATA-LEN = 100 -  WS-SPACE-COUNT 
            MOVE 1                    TO WS-START-POSN   
            MOVE 1                    TO WS-POSN         
            MOVE ZERO                 TO WS-PROCESS-CNT
            PERFORM UNTIL WS-PROCESS-CNT > WS-DATA-LEN   
              IF WS-TEST-DATA(WS-START-POSN:1) = ','     
                 DISPLAY 'WS-TEMP-DATA<'WS-TEMP-DATA'>' 
                 MOVE SPACES          TO WS-TEMP-DATA   
                 MOVE 1               TO WS-POSN         
                 ADD  1               TO WS-START-POSN   
                 ADD  1               TO WS-PROCESS-CNT 
              ELSE                                       
                 IF WS-TEST-DATA(WS-START-POSN:1) = '"' 
                    MOVE 1            TO WS-POSN         
                    MOVE WS-TEST-DATA(WS-START-POSN:1)   
                                      TO WS-TEMP-DATA(WS-POSN:1)     
                    ADD  1            TO WS-START-POSN               
                    ADD  1            TO WS-PROCESS-CNT               
                    PERFORM UNTIL WS-TEST-DATA(WS-START-POSN:1) = '"'
                    OR      WS-PROCESS-CNT > WS-DATA-LEN             
                      ADD 1           TO WS-POSN                     
                      MOVE WS-TEST-DATA(WS-START-POSN:1)             
                                      TO WS-TEMP-DATA(WS-POSN:1)     
                      ADD 1           TO WS-START-POSN               
                      ADD 1           TO WS-PROCESS-CNT               
                    END-PERFORM                                       
                    ADD 1             TO WS-POSN                     
                    MOVE WS-TEST-DATA(WS-START-POSN:1)               
                                      TO WS-TEMP-DATA(WS-POSN:1)     
                    ADD 1             TO WS-START-POSN               
                    ADD 1             TO WS-POSN                     
                 ELSE                                                 
                    MOVE WS-TEST-DATA(WS-START-POSN:1)               
                                      TO WS-TEMP-DATA(WS-POSN:1)   
                    ADD 1             TO WS-POSN                   
                    ADD 1             TO WS-START-POSN             
                    ADD 1             TO WS-PROCESS-CNT             
                 END-IF                                             
              END-IF                                               
            END-PERFORM                                             
            IF WS-TEST-DATA NOT EQUAL TO SPACE                     
                 DISPLAY 'WS-TEMP-DATA<'WS-TEMP-DATA'>'             
            END-IF                                                 
       *                                                           
            STOP RUN.                                               
       *                                                           
        0000-EXIT.                                                 
            EXIT.                                                   
       *                                                           



Output:
Code:

WS-TEMP-DATA<11/21/2012               
WS-TEMP-DATA<                         
WS-TEMP-DATA<3.125%                   
WS-TEMP-DATA<"$172,000.00"             
WS-TEMP-DATA<$0.00                     
WS-TEMP-DATA<"$172,000.00"
Back to top
View user's profile Send private message
Bill Woodger

DFSORT Moderator


Joined: 09 Mar 2011
Posts: 7223

PostPosted: Thu Nov 29, 2012 7:36 am    Post subject: Reply to: Need a help on Unstring
Reply with quote

Here's another one, a bit rough'n'ready:

Code:
       IDENTIFICATION DIVISION.
       PROGRAM-ID. STUBA.
                                                                       
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  W-WHEN-COMPILED                      PIC X(8)BX(8).
       01  W-NUMBER-OF-COLUMNS             COMP PIC 9(4) VALUE 6.
       01  W-DATA-TO-UNSTRING                   PIC X(60).
       01  W-LENGTH-OF-LINE-DISP           COMP PIC 9(4).
       01  W-LENGTH-OF-THIS-COLUMN         COMP PIC 9(4).
       01  W-LENGTH-OF-STRING              COMP PIC 9(4).
       01  W-MAX-THIS-COLUMN               COMP PIC 9(4).
       01  W-COLUMN-DATA-TABLE.
           05  FILLER OCCURS 6 TIMES INDEXED BY WI-CDT-UNSTRING-INDEX.
               10  W-COLUMN-DATA                PIC X(60).
       LINKAGE SECTION.
       01  L-WORK-LINE.
           05  L-WL-LINE-TEXT-DATA.
               10  L-WL-LINE-DISPLACEMENT.
                   15  FILLER OCCURS 0 TO 60 TIMES
                        DEPENDING ON
                         W-LENGTH-OF-LINE-DISP.
                       20  FILLER               PIC X.
               10  L-WL-COLUMN.
                   15  FILLER OCCURS 0 TO 60 TIMES
                        DEPENDING ON
                         W-LENGTH-OF-THIS-COLUMN.
                       20  FILLER               PIC X.
               10  L-NEXT-BYTE-TO-LOOK-AT       PIC X.
                   88  L-NEXT-BYTE-IS-QUOTE     VALUE '"'.
                   88  L-NEXT-BYTE-IS-COMMA     VALUE ','.
                   88  L-NEXT-BYTE-IS-SPACE     VALUE SPACE.
       PROCEDURE DIVISION.
                                                                       
           MOVE WHEN-COMPILED           TO W-WHEN-COMPILED
           DISPLAY "STUBA COMPILED ON " W-WHEN-COMPILED
           MOVE '11/21/2012,,3.125%,"$172,000.00",$0.00,"$172,000.00"'
                                        TO W-DATA-TO-UNSTRING
           PERFORM                      05-DO-THE-WORK
           MOVE SPACE                   TO W-DATA-TO-UNSTRING
           PERFORM                      05-DO-THE-WORK
           MOVE '11/21/2012,,3.125%,"$172,000.00",$0.00,$172000.00XXXXXX
      -    'XXXXX'
                                        TO W-DATA-TO-UNSTRING
           PERFORM                      05-DO-THE-WORK
           MOVE '",,,,,,,,",,3.125%,"$172,000.00",$0.00,"$172,000.00X"'
                                        TO W-DATA-TO-UNSTRING
           PERFORM                      05-DO-THE-WORK
           MOVE ',",,,,,,,",3.125%,"$172,000.00",$0.00,,'
                                        TO W-DATA-TO-UNSTRING
           PERFORM                      05-DO-THE-WORK
           GOBACK
           .
       05-DO-THE-WORK.
           DISPLAY W-DATA-TO-UNSTRING
           SET ADDRESS OF L-WORK-LINE   TO ADDRESS OF
                                            W-DATA-TO-UNSTRING
                                                                       
           PERFORM                      10-FIND-LENGTH-OF-STRING
                                                                       
           SET WI-CDT-UNSTRING-INDEX    TO 1
           MOVE ZERO                    TO W-LENGTH-OF-LINE-DISP
                                           W-LENGTH-OF-THIS-COLUMN
           PERFORM                      20-EXTRACT-COLUMNS
             W-NUMBER-OF-COLUMNS TIMES
           DISPLAY "COL 1>" W-COLUMN-DATA ( 1 ) "<"
           DISPLAY "COL 2>" W-COLUMN-DATA ( 2 ) "<"
           DISPLAY "COL 3>" W-COLUMN-DATA ( 3 ) "<"
           DISPLAY "COL 4>" W-COLUMN-DATA ( 4 ) "<"
           DISPLAY "COL 5>" W-COLUMN-DATA ( 5 ) "<"
           DISPLAY "COL 6>" W-COLUMN-DATA ( 6 ) "<"
           .
       10-FIND-LENGTH-OF-STRING.
           MOVE LENGTH OF W-DATA-TO-UNSTRING
                                        TO W-LENGTH-OF-STRING
                                           W-LENGTH-OF-LINE-DISP
           IF W-DATA-TO-UNSTRING EQUAL TO SPACE
               MOVE ZERO                TO W-LENGTH-OF-STRING
           ELSE
               SUBTRACT 1               FROM W-LENGTH-OF-LINE-DISP
               PERFORM                  10A-WIND-BACK
                 UNTIL                  NOT L-NEXT-BYTE-IS-SPACE
               COMPUTE W-LENGTH-OF-STRING =
                                        W-LENGTH-OF-LINE-DISP
                                        + 1
           END-IF
           DISPLAY "LENGTH >" W-LENGTH-OF-STRING "<"
           .
       10A-WIND-BACK.
           SUBTRACT 1                   FROM W-LENGTH-OF-LINE-DISP
           .
       20-EXTRACT-COLUMNS.
           IF L-NEXT-BYTE-IS-QUOTE
               PERFORM                  20A-FIND-NEXT-QUOTE
               PERFORM                  20D-FIND-NEXT-COMMA
           ELSE
               PERFORM                  20D-FIND-NEXT-COMMA
           END-IF
           MOVE L-WL-COLUMN             TO W-COLUMN-DATA
                                            ( WI-CDT-UNSTRING-INDEX )
           COMPUTE W-LENGTH-OF-LINE-DISP
                                        = W-LENGTH-OF-LINE-DISP
                                        + W-LENGTH-OF-THIS-COLUMN
                                        + 1
           MOVE ZERO                    TO W-LENGTH-OF-THIS-COLUMN
           SET WI-CDT-UNSTRING-INDEX    UP BY 1
           .
       20A-FIND-NEXT-QUOTE.
           ADD 1                        TO W-LENGTH-OF-THIS-COLUMN
           PERFORM                      99A-WIND-ALONG
             UNTIL L-NEXT-BYTE-IS-QUOTE
           .
       20D-FIND-NEXT-COMMA.
           COMPUTE W-MAX-THIS-COLUMN    = W-LENGTH-OF-STRING
                                        - W-LENGTH-OF-LINE-DISP
           PERFORM                      99A-WIND-ALONG
             UNTIL ( W-LENGTH-OF-THIS-COLUMN
                      EQUAL TO W-MAX-THIS-COLUMN )
             OR    L-NEXT-BYTE-IS-COMMA
           .
       99A-WIND-ALONG.
           ADD 1                        TO W-LENGTH-OF-THIS-COLUMN
           .



Output is:

Code:
STUBA COMPILED ON 11/29/12 01.56.03                               
11/21/2012,,3.125%,"$172,000.00",$0.00,"$172,000.00"               
LENGTH >0052<                                                     
COL 1>11/21/2012                                                  <
COL 2>                                                            <
COL 3>3.125%                                                      <
COL 4>"$172,000.00"                                               <
COL 5>$0.00                                                       <
COL 6>"$172,000.00"                                               <
                                                                   
LENGTH >0000<                                                     
COL 1>                                                            <
COL 2>                                                            <
COL 3>                                                            <
COL 4>                                                            <
COL 5>                                                            <
COL 6>                                                            <
11/21/2012,,3.125%,"$172,000.00",$0.00,$172000.00XXXXXXXXXXX       
LENGTH >0060<                                                     
COL 1>11/21/2012                                                  <
COL 2>                                                            <
COL 3>3.125%                                                      <
COL 4>"$172,000.00"                                               <
COL 5>$0.00                                                       <
COL 6>$172000.00XXXXXXXXXXX                                       <
",,,,,,,,",,3.125%,"$172,000.00",$0.00,"$172,000.00X"             
LENGTH >0053<                                                     
COL 1>",,,,,,,,"                                                  <
COL 2>                                                            <
COL 3>3.125%                                                      <
COL 4>"$172,000.00"                                               <
COL 5>$0.00                                                       <
COL 6>"$172,000.00X"                                              <
,",,,,,,,",3.125%,"$172,000.00",$0.00,,                           
LENGTH >0039<                                                     
COL 1>                                                            <
COL 2>",,,,,,,"                                                   <
COL 3>3.125%                                                      <
COL 4>"$172,000.00"                                               <
COL 5>$0.00                                                       <
COL 6>                                                            <
Back to top
View user's profile Send private message
Rohit Umarjikar

Senior Member


Joined: 21 Sep 2010
Posts: 1609
Location: NY,USA

PostPosted: Thu Nov 29, 2012 9:36 pm    Post subject:
Reply with quote

Thanks all, I wil try this out.
Also, the other way around is to formate the input file like,

Code:
11/21/2012;;3.125%;"$172,000.00";$0.00;"$172,000.00"
Back to top
View user's profile Send private message
Pandora-Box

Moderator


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

PostPosted: Thu Nov 29, 2012 9:50 pm    Post subject:
Reply with quote

If you could have done that at first hand you could have saved some time yourself and of others too

Yet this thread has very useful contents posted by other experts
Back to top
View user's profile Send private message
Rohit Umarjikar

Senior Member


Joined: 21 Sep 2010
Posts: 1609
Location: NY,USA

PostPosted: Thu Nov 29, 2012 10:26 pm    Post subject:
Reply with quote

With the the help of SORT-PARSE can you please guide to change the below file

Code:
11/21/2012,,3.125%,"$172,000.00",$0.00,"$172,000.00"



to

Code:
11/21/2012;;3.125%;"$172,000.00";$0.00;"$172,000.00"
Back to top
View user's profile Send private message
Akatsukami

Global Moderator


Joined: 03 Oct 2009
Posts: 1738
Location: Bloomington, IL

PostPosted: Thu Nov 29, 2012 10:43 pm    Post subject:
Reply with quote

Rohit Umarjikar wrote:
With the the help of SORT-PARSE can you please guide to change the below file

Code:
11/21/2012,,3.125%,"$172,000.00",$0.00,"$172,000.00"



to

Code:
11/21/2012;;3.125%;"$172,000.00";$0.00;"$172,000.00"

Yes. Take the manual and beat the producer of the data until heesh agrees to change the delimiter. Otherwise, you have four solutions (my pseudo-code, Dr. Sorichetti's Rexx script, and Sr. Woodger's and Sri Sai's COBOL programs); it is useless to try to put off having to actually do some work.
Back to top
View user's profile Send private message
Pandora-Box

Moderator


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

PostPosted: Thu Nov 29, 2012 10:50 pm    Post subject:
Reply with quote

how is the Input file created?

Cant you change the delimiters to something else unless there is no impacts?
Back to top
View user's profile Send private message
Rohit Umarjikar

Senior Member


Joined: 21 Sep 2010
Posts: 1609
Location: NY,USA

PostPosted: Thu Nov 29, 2012 10:54 pm    Post subject:
Reply with quote

Quote:
how is the Input file created?

Cant you change the delimiters to something else unless there is no impacts?


No actually, We are getting the feed from some application and they dump it on our server with the format I mentioned thru csv file. Now I am writing a program to validate the data by doing unstring with deliminator as ",". So hence the problem.[/quote]
Back to top
View user's profile Send private message
Pandora-Box

Moderator


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

PostPosted: Thu Nov 29, 2012 11:02 pm    Post subject:
Reply with quote

I still repeat my question Why cant that application change delimiter

or send the data as fixed length



As you care a damn for solutions given to you icon_evil.gif
Back to top
View user's profile Send private message
Rohit Umarjikar

Senior Member


Joined: 21 Sep 2010
Posts: 1609
Location: NY,USA

PostPosted: Thu Nov 29, 2012 11:53 pm    Post subject:
Reply with quote

The upstream process is not owned by us so they have there layouts fixed as per the requirement.So we as a receiver have to take care of these layouts.
Back to top
View user's profile Send private message
dick scherrer

Site Director


Joined: 23 Nov 2006
Posts: 19270
Location: Inside the Matrix

PostPosted: Fri Nov 30, 2012 2:01 am    Post subject:
Reply with quote

Hello,

You need to re-read what Akatsukami (and others) provided.

You have been given ways to get what you need done.

It is now time for you to "Just Do It" . . .
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> COBOL Programming All times are GMT + 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts Unstring list of values into an array. Roshnii COBOL Programming 8 Mon Jun 27, 2016 6:25 pm
No new posts Problem with UNSTRING of CSV data Bill Woodger COBOL Programming 14 Tue Dec 15, 2015 5:32 pm
No new posts Unstring in Cobol Govardhana krishna COBOL Programming 7 Thu Oct 17, 2013 8:52 pm
No new posts Unstring the message d katkuri COBOL Programming 13 Wed Aug 14, 2013 12:28 pm
No new posts UNSTRING of Variable length Data kedianirmal COBOL Programming 9 Thu Jul 18, 2013 3:25 pm


Facebook
Back to Top
 
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us