find date in a string



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

find date in a string

Postby Raja190 » Wed Dec 30, 2015 4:00 pm

Hi,

I have a requirement where i need to find out a date in a specific string which is not always in a same column.

In the below mentioned strings i need to just pull the date alone which is not stays in same column and i thought of using U/A to find the dates.


Please suggest me a way to find the dates here in the below string.


Example.
Example1: xxxxxxxxxxxxxxxx U/A  01/012015 xxxxxxxxx
Example2: XXXXXXXXU/AXXXXXXXXXXU/A 01/04/12 XXXXXXXXXX
Example3: XXXXXXXXXXXXXXXX U/A  10/19/13 XXXXXXXXXXXX U/A XXXXXX

coded for better readability



Thanks !
Raja190
 
Posts: 35
Joined: Mon Dec 14, 2015 8:13 pm
Has thanked: 0 time
Been thanked: 0 time

Re: find date in a string

 

Re: find date in a string

Postby Aki88 » Mon Jan 04, 2016 1:39 pm

Hello,

If you always have a 'U/A' before the date, then this request can be easily accomplished using SORT's PARSE function, check relevant documentation DFSORT/SYNCSORT depending on what your site uses. In case you can have multiple 'U/A' in a record, with not all of them having a date after it, then you'll have to group the items such that you can uniquely identify the starting character position of the date.

There are numerous examples of PARSE available on the forum; you can also refer the DFSORT manual for usage of PARSE function here.

Hth.

<Edited>
Aki88
 
Posts: 303
Joined: Tue Jan 28, 2014 1:52 pm
Has thanked: 32 times
Been thanked: 27 times

Re: find date in a string

Postby BillyBoyo » Mon Jan 04, 2016 3:40 pm

If the data you want (a date, apparently, although your first example isn't a usual style of date) is always preceded by "U/A " and that is always the first "U/A " for each lump of data then in COBOL you can use UNSTRING to "split" the lump of data at that point, and a second UNSTRING with space as a delimiter to isolate the variable-length date.
BillyBoyo
Global moderator
 
Posts: 3776
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 257 times

Re: find date in a string

Postby Raja190 » Mon Jan 04, 2016 5:55 pm

Hi Billy ,


Thanks for your reply.

I think my example is not clear. let me re-struct it.

here I meant to say, there is a chance of more than one spaces before the intial 'U/A' also anywhere between the strings. also the 'U/A' could come at any column of the string.
All that i wanted is the date that is standing in between this long string.


Example1: xxxxx xxxxxx xxxxx U/A 01/01/2015 xxxxxxxxx
Example2: XX XXX XXX U/A XXX XXXXXX XU/A 01/04/12 XXXXXXXXXX
Example3: XXXXX XXXXXXXXXXX U/A 10/19/13 XXXX XXXXXXX X U/A XXXX XX

coded for better readability


Thanks
Raja.R
Raja190
 
Posts: 35
Joined: Mon Dec 14, 2015 8:13 pm
Has thanked: 0 time
Been thanked: 0 time

Re: find date in a string

Postby BillyBoyo » Mon Jan 04, 2016 6:48 pm

You are going to have to search for the U/A. Then check whether the data which follows it conforms to one your two date-patterns. If yes, you have your date, if not, keep searching. If the dates are guaranteed to be dates, that's about it. If not, you have to check that the date is also valid.

If there are a limited number of U/A which may appear, it is still feasible with UNSTRING, else there are about three ways to code it out.
BillyBoyo
Global moderator
 
Posts: 3776
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 257 times

Re: find date in a string

Postby Raja190 » Mon Jan 04, 2016 6:54 pm

Bill,

I guess un-string won't work in all the situation because the text before 'u/a' is just names which may occur with many spaces with and some times with special characters which may vary depend on what users enters. also there is a chance of more than one 'u/a' before and after the date. between this I need to find this string which would be a valid date.


What are all the 3 ways that you are suggesting ?
Raja190
 
Posts: 35
Joined: Mon Dec 14, 2015 8:13 pm
Has thanked: 0 time
Been thanked: 0 time

Re: find date in a string

Postby BillyBoyo » Mon Jan 04, 2016 7:03 pm

The UNSTRING approach will work unless you have the possibility of "lots" of U/As. Spaces do not matter. "Lots" realistically means not-too-many-more-than-three. Actually, if you have lots, you can use UNSTRING in a loop.

My favoured approach would be with variable-length field(s). Another approach would be using indexes. The third, and probably most popular these days, would be using reference-modification. Personally I don't "like" reference-modification because its use tends to obscure what is going on with the code versus the other approaches.
BillyBoyo
Global moderator
 
Posts: 3776
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 257 times

Re: find date in a string

Postby UserName » Tue Jan 05, 2016 4:03 am

It seems you only need to find the date.

Use SQUEEZE from Dfsort, so that there are no spaces in between. Suppose it is like "U/AMM/DD/YYYY". You can then use PARSE as the position of date is fixed in relation to U/A. You can know it's a date after U/A if you find a '/' at 3rd and 6th positions after U/A.

Hope that helps.
UserName
 
Posts: 3
Joined: Thu Dec 17, 2015 10:30 pm
Has thanked: 0 time
Been thanked: 0 time

Re: find date in a string

Postby BillyBoyo » Tue Jan 05, 2016 5:01 am

That would require a PARSEd field for each possible occurrence of U/A. Because DFSORT has no looping constructs, you have to code each one out. The code can be generated. Further minor problem is that there are two date formats, of different lengths (two- and four-digit years).

I don't think you'd need the SQZ itself. PARSE for the U/A, then PARSE for STARTAFT=BLANKS.
BillyBoyo
Global moderator
 
Posts: 3776
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 257 times


Return to IBM Cobol

 


  • Related topics
    Replies
    Views
    Last post