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

Reading the CSV data in COBOL and move it to PACKED-DECIMAL


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

New User


Joined: 13 Jul 2018
Posts: 8
Location: Nederlands

PostPosted: Tue Dec 03, 2019 5:13 pm
Reply with quote

Hi Team,

In my one of the code, I am reading the data from CSV file and assigning to working fields by Unstrung function delimited by ';'.

Issue:
When I am reading the file for the decimal values (like 2.33 / 11.03 .. etc) data is not populating properly if i define working field as 9(2)V9(2)
So i had define as X(5) and doing the data population.
Code:

01-Working.
03 CSV-EE-WRDE1                 PIC 9(02)V9(2).
03 CSV-EE-WRDE2                 PIC S9(02)V9(2)

01 CSV-FILE
03 CSV-EE-ST-DATUM           PIC X(10).  
03 CSV-EE-WRDE                 PIC X(05).       <--- which is decimal like 2.33  
03 CSV-KOOPSOM                PIC X(13).

Procedure:
UNSTRING CSV-RECORD            
    DELIMITED BY C-DELIMITER    
              OR  ALL LOW-VALUES
    INTO CSV-EE-ST-DATUM,      
            CSV-EE-WRDE
            CSV-KOOPSOM
    TALLYING IN H-NO-OF-FIELDS
END-UNSTRING.                




But later below steps, i need to insert the data to table field
Code:


Move  CSV-EE-WRDE  to CSV-EE-WRDE1      <---- move is not populating as expected
Move  CSV-EE-WRDE1  to CSV-EE-WRDE2    <---- move is not populating as expected

Table Move

Move  SV-EE-ST-DATUM   TO   EINDLABEL-LABEL
Move   CSV-EE-WRDE2      TO  CONTRACTValue
Move CSV-KOOPSOM       TO   RUBRIEK              

INSERT INTO Table-Name (field1, field2, field3) from (EINDLABEL-LABEL,CONTRACTValue ,RUBRIEK      )

 




Code:

Table field declaration ( which is already existed, alter not possible ) 
10 EINDLABEL-LABEL      PIC X(10).                        
10 CONTRACTValue        PIC 9(02)V9(02) USAGE PACKED-DECIMAL.
10 RUBRIEK                    PIC X(13).

 


Could any one help me getting proper values for the decimal part.


Thanks in advance.
Back to top
View user's profile Send private message
Robert Sample

Global Moderator


Joined: 06 Jun 2008
Posts: 8700
Location: Dubuque, Iowa, USA

PostPosted: Tue Dec 03, 2019 7:27 pm
Reply with quote

You apparently do not understand that a V in a PICTURE clause (like 9(2)V9(2) for example) means that the decimal point does NOT appear -- it is implied. And an actual decimal point in the data will cause problems since it is NOT implied.

If you are sure that your data will ALWAYS be 2 digits followed by a decimal point followed by 2 digits, use PIC 9(2).9(2) so the decimal point is explicit. If there is a chance of the data not being 2 digits followed by a decimal point followed by 2 digits, use the FUNCTION NUMVAL to convert an X(5) into numeric data.
Back to top
View user's profile Send private message
sergeyken

Senior Member


Joined: 29 Apr 2008
Posts: 2144
Location: USA

PostPosted: Wed Dec 04, 2019 5:58 pm
Reply with quote

Besides of already mentioned problem with COBOL format including 'V' marker, there is another, more serious problem, particularly with CSV data: the length, and position of each field are not fixed (otherwise it wouldn't be a CSV, but fixed format records).

The TS did not give any example of his input data. For CSV we might assume
Code:
 1234 , 56 , 789 , 10 , 5 , .....

or
Code:
 1234 ; 56 ; 789 ; 10 ; 5 ; .....

or
Code:
 1234 | 56 | 789 | 10 | 5 | .....

or
Code:
"1234";"56";"789";"10";"5"; .....

or
Code:
"1.34";"-5.6";"+78.9";"-1.0";"+.5"; .....

etc, etc, etc.

In general case it might be a sophisticated task, especially in COBOL, to parse free text of any possible CSV version.
If input data was better formalyzed, it would be much easier to process it.
Back to top
View user's profile Send private message
Rohit Umarjikar

Global Moderator


Joined: 21 Sep 2010
Posts: 3076
Location: NYC,USA

PostPosted: Wed Dec 04, 2019 8:45 pm
Reply with quote

Quote:
But later below steps, i need to insert the data to table field


Easiest way is to use DB2 DEC (Decimal) function during the INSERT or prior and move the value to corresponding field in DCLGEN. This is code is clean and no convoluted COBOL logic for such numeric conversions.

Code:
03 CSV-EE-WRDE                 PIC X(05).

changes to
Code:
DEC(:CSV-EE-WRDE,4,2)


If you don't have DB2 then use DFSORT to achieve this.
Back to top
View user's profile Send private message
sergeyken

Senior Member


Joined: 29 Apr 2008
Posts: 2144
Location: USA

PostPosted: Wed Dec 04, 2019 9:05 pm
Reply with quote

Rohit Umarjikar wrote:
If you don't have DB2 then use DFSORT to achieve this.

I agree. If the author is not afraid of SORT tools, the easiest way would be: first to convert CSV data to flat file with fixed fields of desired format, and then to read this normalized file without problems straightforward into COBOL variables with matching definition.
It might be much more simple rather than parsing free-text CSV data in COBOL code, unless some ready-to-use service procedure is available for this conversion.
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 Help required to reset decimal points... DFSORT/ICETOOL 10
No new posts Unable to interpret a hex value to De... COBOL Programming 7
No new posts COBOL sorting, with input GDG base COBOL Programming 7
No new posts Need help with ADABAS query (COBOL-AD... All Other Mainframe Topics 0
No new posts db2 vs static data COBOL Programming 1
Search our Forums:

Back to Top