UNSTRING delimited by comma



Support for OS/VS COBOL, VS COBOL II, COBOL for OS/390 & VM and Enterprise COBOL for z/OS

UNSTRING delimited by comma

Postby Ashok Raju » Tue Nov 15, 2011 2:43 pm

Hi,

I have a problem with a comma delimiter file.

A comma will separate each field. In a cobol program "UNSTRING delimited by comma" is used to move to working storage section variables and the count is stored in respective cnt-edt variable. Later each field length(using cnt-edt values) is validated as per given requirement.

The problem is
"At the end of last field there will not be any comma. So the spaces after the fields are also being counted. Because of this the field length validation for the last field is failing"

I thought to use some SPACES also as delimiter, but I think it is bad programming.

I don't know whether there is any possibility to find end of the record.

please suggest.

Thanks,
Raju
Ashok Raju
 
Posts: 13
Joined: Tue Oct 25, 2011 3:45 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Delimiter.

Postby BillyBoyo » Tue Nov 15, 2011 2:53 pm

Can you show the UNSTRING code you are using?

One thing you could try with your last field is using intrinsic function REVERSE and then use INSPECT to count the leading spaces.
BillyBoyo
Global moderator
 
Posts: 3804
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 265 times

Re: Delimiter.

Postby Ashok Raju » Tue Nov 15, 2011 3:44 pm

BillyBoyo,

the UNSTRING CODE is

UNSTRING INP-REC DELIMITED BY ','
   INTO ABC-ID      COUNT IN  CNT-EDT (01)
        ABC-NAME    COUNT IN  CNT-EDT (02)
        ABC-DES     COUNT IN  CNT-EDT (03)
        ABC-MSC    COUNT IN  CNT-EDT (04)
        ABC-FNL     COUNT IN  CNT-EDT (05)
   TALLYING IN CNT-EDT (06)


the records in the file are in variable format
    11111,name1,desc abc,,1234<spaces>
    22222,name2,desc eos,1,3567<spaces>
    33333,name3,adsfl e ,,5849<spaces>
Ashok Raju
 
Posts: 13
Joined: Tue Oct 25, 2011 3:45 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Delimiter.

Postby BillW » Tue Nov 15, 2011 5:14 pm

Here is example from IBM Cobol manual:

UNSTRING INV-RCD
DELIMITED BY ALL SPACES OR "/" OR DBY-1
INTO ITEM-NAME COUNT IN CTR-1
INV-NO DELIMITER IN DLTR-1 COUNT IN CTR-2
INV-CLASS
M-UNITS COUNT IN CTR-3
FIELD A
DISPLAY-DOLS DELIMITER IN DLTR-2 COUNT IN CTR-4
WITH POINTER CHAR-CT
TALLYING IN FLDS-FILLED
ON OVERFLOW GO TO UNSTRING-COMPLETE.

Note the fact that one can use multiple delimiters in the statement.

http://publibz.boulder.ibm.com/cgi-bin/ ... 2302&CASE=
BillW
 
Posts: 20
Joined: Thu Nov 10, 2011 8:21 am
Has thanked: 0 time
Been thanked: 3 times

Re: Delimiter.

Postby BillyBoyo » Tue Nov 15, 2011 5:47 pm

Unless your INP-REC is variable in length, you will get exactly the problem you describe, the final field will include whatever is "padding" to the end of the field you are UNSTRINGing from, so you will need to adjust for that.

You cannot include SPACE as a delimiter in this UNSTRING because spaces are valid in your data.

You can using a second UNSTRING delimited by space for your final field to get the correct chacter count. Or the reverse/inspect already suggested. Or by "counting backwards" on the final field until you get a non-blank. Etc. Lots of ways to do it.

Are you certain that you can get no commas in the "name" or any other field? If you can, that will wreck your code. If you should only have four commas I'd use INSPECT with TALLYING to check all is OK before the UNSTRING.

It is good practice with UNSTRING to initialise all your target fields before executing it, including the counts.

Why are you subscripting the counts?

If you discover that commas are valid in the data (you'll probably see then that the field is withing quotes) then you have something which is becoming too complex for UNSTRING. At which point you might consider pre-processing with your Sort product or coding things with a different technique.
BillyBoyo
Global moderator
 
Posts: 3804
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 265 times

Re: Delimiter.

Postby Ashok Raju » Tue Nov 15, 2011 8:39 pm

BillyBoyo,

I did the same. I moved final field to temp variable and then using "UNSTRING TEMP delimited by spaces" got expected output. But I am expecting some simple logic instead of doing it in 2 different steps.

The file is .CSV file which will be received using FTP. So I dont think the commas will be in between names or any other fields.

There are more than 20 fields not 4. I just gave an example so that the problem can be understand easily.

The counts are main in this program because the length of each field should be validated and if it fails length validation, that record is considered as error record.

Or the reverse/inspect already suggested. Or by "counting backwards" on the final field until you get a non-blank. Etc. Lots of ways to do it.


can you please tell me how this can be done by above quotes.

Thanks,
Raju
Ashok Raju
 
Posts: 13
Joined: Tue Oct 25, 2011 3:45 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Delimiter.

Postby BillyBoyo » Tue Nov 15, 2011 9:03 pm

Look in your Cobol manuals for the intrinsic functions. There is one called REVERSE which will reverse the order of the bytes in the field that you give it. Ensure the reversed value goes into a new field (you don't want to disturb the original value). The look up INSPECT with TALLYING which you can use to count the LEADING SPACES.

OR, put your data into another field which is defined with a one-byte long OCCURS field-length (literal of the field length) times. Start your subscript/index from the length and subtract from it until you get a non-blank. You will then be able to calculate the length of the leading part of the field without the trailing blanks. Look out for the field being "full" (last character not blank) and "empty", field is equal to SPACE.
BillyBoyo
Global moderator
 
Posts: 3804
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 265 times


Return to IBM Cobol

 


  • Related topics
    Replies
    Views
    Last post