Page 1 of 1

find date in a string

PostPosted: Wed Dec 30, 2015 4:00 pm
by Raja190
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 !

Re: find date in a string

PostPosted: Mon Jan 04, 2016 1:39 pm
by Aki88
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>

Re: find date in a string

PostPosted: Mon Jan 04, 2016 3:40 pm
by BillyBoyo
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.

Re: find date in a string

PostPosted: Mon Jan 04, 2016 5:55 pm
by Raja190
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

Re: find date in a string

PostPosted: Mon Jan 04, 2016 6:48 pm
by BillyBoyo
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.

Re: find date in a string

PostPosted: Mon Jan 04, 2016 6:54 pm
by Raja190
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 ?

Re: find date in a string

PostPosted: Mon Jan 04, 2016 7:03 pm
by BillyBoyo
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.

Re: find date in a string

PostPosted: Tue Jan 05, 2016 4:03 am
by UserName
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.

Re: find date in a string

PostPosted: Tue Jan 05, 2016 5:01 am
by BillyBoyo
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.