| Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#1
| |||
| |||
| I have a situation in COBOL where I need to take a string and pull characters out of it. The characters I need to pull depend on the length of the target field, which could be 6, 8, or 10 bytes. The source string is 30 bytes. An example may help... SOURCE STRING: "19/03/2007 17:21:05:83" REQUIRED RESULTS: 6 byte = 190307 8 byte = 19032007 10 byte = 19/03/2007 Obviously, we can discount the 10 byte case as a simple move will do it. There are several immediately obvious approaches, but I was hoping for something smarter. 1. The old REFMOD solution... move space to target evaluate function LENGTH (target) when 6 move source (1:2) to target move source (4:2) to target move source (7:2) to target when 8 move source (1:2) to target move source (4:2) to target move source (7:4) to target when other move source to target end-evaluate 2. The old MOVE CORR solution... 01 source. 12 dd pic xx. 12 filler pic x. 12 mm pic xx. 12 filler pic x. 12 cc pic xx. 12 yy pic xx. 12 filler pic x(20). 01 target-6. 12 dd pic xx. 12 mm pic xx. 12 yy pic xx. 01 target-8. 12 dd pic xx. 12 mm pic xx. 12 cc pic xx. 12 yy pic xx. .... move space to target evaluate function LENGTH (target) when 6 move CORRESPONDING source to target-6 move target-6 to target when 8 move CORRESPONDING source to target-8 move target-8 to target when other move source to target end-evaluate While both of these will do the job, there's a fair bit of writing and they leave me feeling "uncomfortable"... It SHOULD be possible to simply strip out the slashes... As far as I can ascertain the INSPECT ...TRANSFORMING feature which Robert recently reminded us about, will not transform something to nothing... Both of the above will work correctly whether the system is using DD/MM/YYYY or MM/DD/YYYY except that the names for day and month will be swapped, another thing which, while not important in the context, leaves a bad taste... Any thoughts? Pete. -- "I used to write COBOL...now I can do anything." |
|
#2
| |||
| |||
| Pete, I am NOT sure of this (and haven't tried it on any compiler), but I *think* that many compilers do not "validate" numeric-edited symbols. Therefore, if you defined the first 6 bytes as a numeric-edited field with: Pic 99,99,9999 you could move that to a numeric field and "lose" the slashes. I am not certain, but some compilers may even have a way of treating "/" as a valid numeric-editing symbol. -- Bill Klein wmklein <at> ix.netcom.com "Pete Dashwood" <dashwood@removethis.enternet.co.nz> wrote in message news:6g20pvFdnc3oU1@mid.individual.net... >I have a situation in COBOL where I need to take a string and pull characters >out of it. > > The characters I need to pull depend on the length of the target field, which > could be 6, 8, or 10 bytes. > > The source string is 30 bytes. > > An example may help... > > SOURCE STRING: "19/03/2007 17:21:05:83" > > REQUIRED RESULTS: > > 6 byte = 190307 > 8 byte = 19032007 > 10 byte = 19/03/2007 > > Obviously, we can discount the 10 byte case as a simple move will do it. > > There are several immediately obvious approaches, but I was hoping for > something smarter. > > 1. The old REFMOD solution... > > move space to target > evaluate function LENGTH (target) > when 6 > move source (1:2) to target > move source (4:2) to target > move source (7:2) to target > when 8 > move source (1:2) to target > move source (4:2) to target > move source (7:4) to target > when other > move source to target > end-evaluate > > 2. The old MOVE CORR solution... > > 01 source. > 12 dd pic xx. > 12 filler pic x. > 12 mm pic xx. > 12 filler pic x. > 12 cc pic xx. > 12 yy pic xx. > 12 filler pic x(20). > > 01 target-6. > 12 dd pic xx. > 12 mm pic xx. > 12 yy pic xx. > > 01 target-8. > 12 dd pic xx. > 12 mm pic xx. > 12 cc pic xx. > 12 yy pic xx. > ... > > move space to target > evaluate function LENGTH (target) > when 6 > move CORRESPONDING source to target-6 > move target-6 to target > when 8 > move CORRESPONDING source to target-8 > move target-8 to target > when other > move source to target > end-evaluate > > > While both of these will do the job, there's a fair bit of writing and they > leave me feeling "uncomfortable"... It SHOULD be possible to simply strip out > the slashes... > > As far as I can ascertain the INSPECT ...TRANSFORMING feature which Robert > recently reminded us about, will not transform something to nothing... > > Both of the above will work correctly whether the system is using DD/MM/YYYY > or MM/DD/YYYY except that the names for day and month will be swapped, another > thing which, while not important in the context, leaves a bad taste... > > Any thoughts? > > Pete. > -- > "I used to write COBOL...now I can do anything." > |
|
#3
| |||
| |||
| On Aug 8, 4:36*pm, "Pete Dashwood" <dashw...@removethis.enternet.co.nz> wrote: > I have a situation in COBOL where I need to take a string and pull > characters out of it. > > The characters I need to pull depend on the length of the target field, > which could be 6, 8, or 10 bytes. > > The source string is 30 bytes. > > An example may help... > > SOURCE STRING: *"19/03/2007 *17:21:05:83" > > REQUIRED RESULTS: > > 6 * *byte = *190307 > 8 * *byte = *19032007 > 10 *byte = *19/03/2007 > > Obviously, we can discount the 10 byte case as a simple move will do it. > > There are several immediately obvious approaches, but I was hoping for > something smarter. > > 1. The old REFMOD solution... > > * * move space to target > * * evaluate function LENGTH *(target) > * * * * *when 6 > * * * * * * * * * *move source (1:2) to target > * * * * * * * * * *move source (4:2) to target > * * * * * * * * * *move source (7:2) to target > * * * * *when *8 > * * * * * * * * * *move source (1:2) to target > * * * * * * * * * *move source (4:2) to target > * * * * * * * * * *move source (7:4) to target > * * * * *when *other > * * * * * * * * * move source to target > * * *end-evaluate > > 2. The old MOVE CORR solution... > > 01 *source. > * * * 12 dd * *pic xx. > * * * 12 filler *pic x. > * * * 12 mm *pic xx. > * * * 12 filler *pic x. > * * * 12 cc * * pic xx. > * * * 12 yy * * pic xx. > * * * 12 filler *pic x(20). > > 01 *target-6. > * * * 12 dd * *pic xx. > * * * 12 mm *pic xx. > * * * 12 yy * * pic xx. > > 01 *target-8. > * * * 12 dd * *pic xx. > * * * 12 mm *pic xx. > * * * 12 cc * * pic xx. > * * * 12 yy * * pic xx. > ... > > * * move space to target > * * evaluate function LENGTH *(target) > * * * * *when 6 > * * * * * * * * * *move CORRESPONDING source *to target-6 > * * * * * * * * * *move target-6 to target > * * * * *when *8 > * * * * * * * * * *move CORRESPONDING source *to target-8 > * * * * * * * * * *move target-8 to target > * * * * *when *other > * * * * * * * * * move source to target > * * *end-evaluate > > While both of these will do the job, there's a fair bit of writing and they > leave me feeling "uncomfortable"... It SHOULD be possible to simply strip > out the slashes... > > As far as I can ascertain the INSPECT ...TRANSFORMING feature which Robert > recently reminded us about, will not transform something to nothing... > > Both of the above will work correctly whether the system is using DD/MM/YYYY > or MM/DD/YYYY except that the names for day and month will be swapped, > another thing which, while not important in the context, leaves a bad > taste... > > Any thoughts? It makes me wonder how FUNCTION LENGTH(Target) could provide anything useful. Perhaps your code is to be COPYed into various programs where Target will be defined. Or perhaps Target could be 'DEPENDING ON', or will it be done with ANY LENGTH ? when 6 move source (1:2) to target move source (4:2) to target move source (7:2) to target Granted it is easy to forget the (3:2) etc on Target, how about: evaluate when 6 string source(1:2) source(4:2) source(7:2) into target when 8 string source(1:2) source(4:2) source(5:4) into target when other move source(1:10) to target end-evaluate |
|
#4
| |||
| |||
| "William M. Klein" <wmklein@nospam.netcom.com> schrieb im Newsbeitrag news:eVQmk.403263$fz6.244668@fe08.news.easynews.co m... > Pete, > I am NOT sure of this (and haven't tried it on any compiler), but I > *think* that many compilers do not "validate" numeric-edited symbols. > Therefore, if you defined the first 6 bytes as a numeric-edited field > with: > Pic 99,99,9999 > you could move that to a numeric field and "lose" the slashes. > > I am not certain, but some compilers may even have a way of treating "/" > as a valid numeric-editing symbol. > I did a test with out COBOL2000 Compiler (which does not yet support the EC-DATA-INCOMPATIBLE exception condition) with following definitions: 01 TARGET-6 PIC 9(6). 01 TARGET-8 PIC 9(8). 01 XXX PIC X(30) VALUE "19/03/2007 17:21:05:83". 01 XXX8 REDEFINES XXX PIC 99/99/9999. 01 XXX6 REDEFINES XXX PIC 99/99/BB99. following code runs without problems MOVE XXX8 TO TARGET-8 DISPLAY "T8 >" TARGET-8 MOVE XXX6 TO TARGET-6 DISPLAY "T6 >" TARGET-6 and gives the expected results: T8 >19032007 T6 >190307 Karl Kiesel Fujitsu Siemens Computers, München |
|
#5
| |||
| |||
| "Pete Dashwood" <dashwood@removethis.enternet.co.nz> wrote in message news:6g20pvFdnc3oU1@mid.individual.net... > I have a situation in COBOL where I need to take a string and pull > characters out of it. > > The characters I need to pull depend on the length of the target field, > which could be 6, 8, or 10 bytes. > > The source string is 30 bytes. > > An example may help... > > SOURCE STRING: "19/03/2007 17:21:05:83" > > REQUIRED RESULTS: > > 6 byte = 190307 > 8 byte = 19032007 > 10 byte = 19/03/2007 > > Obviously, we can discount the 10 byte case as a simple move will do it. > > There are several immediately obvious approaches, but I was hoping for > something smarter. [snip] > As far as I can ascertain the INSPECT ...TRANSFORMING feature which Robert > recently reminded us about, will not transform something to nothing... [snip] -----Code using INSPECT ... CONVERTING $set ans85 flag"ans85" flagas"s" identification division. program-id. A27B38. data division. working-storage section. 01 source-string pic x(30) value "19/03/2007 17:21:05.83". 01 source-pattern pic x(30) value "Dd/Mm\CcYy Hh:Nn;Ss.Tt". 01 target-patterns. 03 target-6 pic x(6) value "DdMmYy". 03 target-8 pic x(8) value "DdMmCcYy". 03 target-10 pic x(10) value "Dd/Mm\CcYy". 01 target-length binary pic 9(4) value 0. 01 target. 03 pic x occurs 6 to 10 depending target-length. procedure division. begin. perform 10 times compute target-length = (function integer (function random * 3)) * 2 + 6 evaluate function length (target) when 6 move target-6 to target when 8 move target-8 to target when 10 move target-10 to target end-evaluate inspect target converting source-pattern to source-string display target end-perform stop run |
|
#6
| |||
| |||
| "William M. Klein" <wmklein@nospam.netcom.com> wrote in message news:eVQmk.403263$fz6.244668@fe08.news.easynews.co m... > Pete, > I am NOT sure of this (and haven't tried it on any compiler), but I > *think* that many compilers do not "validate" numeric-edited symbols. > Therefore, if you defined the first 6 bytes as a numeric-edited field > with: > Pic 99,99,9999 Sorry Bill, in what way is that a 6 byte field...? I know that some compilers do allow / as an insertion but how do you 'de-edit' an editted field? I have aways understood that that was not permitted. (example: move pic 99/99/9999 to 99999999 ... I think this will give an illegal move... > you could move that to a numeric field and "lose" the slashes. > I don't think so. Can you post a detailed working example, please? > I am not certain, but some compilers may even have a way of treating "/" > as a valid numeric-editing symbol. > Yes, I remember a couple of old mainframe compilers that supported this. Pete. -- "I used to write COBOL...now I can do anything." |
|
#7
| |||
| |||
| "Karl Kiesel" <Karl.Kiesel@fujitsu-siemens.com> wrote in message news:g7gpg9$ph0$1@nntp.fujitsu-siemens.com... > "William M. Klein" <wmklein@nospam.netcom.com> schrieb im Newsbeitrag > news:eVQmk.403263$fz6.244668@fe08.news.easynews.co m... >> Pete, >> I am NOT sure of this (and haven't tried it on any compiler), but I >> *think* that many compilers do not "validate" numeric-edited symbols. >> Therefore, if you defined the first 6 bytes as a numeric-edited field >> with: >> Pic 99,99,9999 >> you could move that to a numeric field and "lose" the slashes. >> >> I am not certain, but some compilers may even have a way of treating "/" >> as a valid numeric-editing symbol. >> > > I did a test with out COBOL2000 Compiler (which does not yet support the > EC-DATA-INCOMPATIBLE exception condition) with following definitions: > 01 TARGET-6 PIC 9(6). > 01 TARGET-8 PIC 9(8). > 01 XXX PIC X(30) VALUE "19/03/2007 17:21:05:83". > 01 XXX8 REDEFINES XXX PIC 99/99/9999. > 01 XXX6 REDEFINES XXX PIC 99/99/BB99. > following code runs without problems > MOVE XXX8 TO TARGET-8 > DISPLAY "T8 >" TARGET-8 > MOVE XXX6 TO TARGET-6 > DISPLAY "T6 >" TARGET-6 > and gives the expected results: > T8 >19032007 > T6 >190307 > > Karl Kiesel > Fujitsu Siemens Computers, München > Thank you for this, Karl. Very impressive. I would not have expected that to work. Is this because of the 2002 standard or would you expect prior compilers to work as well? Pete. -- "I used to write COBOL...now I can do anything." > > |
|
#8
| |||
| |||
| "Richard" <riplin@azonic.co.nz> wrote in message news:2d8bc11e-dffa-41b7-b314-b995b0bb04ac@z11g2000prl.googlegroups.com... On Aug 8, 4:36 pm, "Pete Dashwood" <dashw...@removethis.enternet.co.nz> wrote: > I have a situation in COBOL where I need to take a string and pull > characters out of it. > > The characters I need to pull depend on the length of the target field, > which could be 6, 8, or 10 bytes. > > The source string is 30 bytes. > > An example may help... > > SOURCE STRING: "19/03/2007 17:21:05:83" > > REQUIRED RESULTS: > > 6 byte = 190307 > 8 byte = 19032007 > 10 byte = 19/03/2007 > > Obviously, we can discount the 10 byte case as a simple move will do it. > > There are several immediately obvious approaches, but I was hoping for > something smarter. > > 1. The old REFMOD solution... > > move space to target > evaluate function LENGTH (target) > when 6 > move source (1:2) to target > move source (4:2) to target > move source (7:2) to target > when 8 > move source (1:2) to target > move source (4:2) to target > move source (7:4) to target > when other > move source to target > end-evaluate > > 2. The old MOVE CORR solution... > > 01 source. > 12 dd pic xx. > 12 filler pic x. > 12 mm pic xx. > 12 filler pic x. > 12 cc pic xx. > 12 yy pic xx. > 12 filler pic x(20). > > 01 target-6. > 12 dd pic xx. > 12 mm pic xx. > 12 yy pic xx. > > 01 target-8. > 12 dd pic xx. > 12 mm pic xx. > 12 cc pic xx. > 12 yy pic xx. > ... > > move space to target > evaluate function LENGTH (target) > when 6 > move CORRESPONDING source to target-6 > move target-6 to target > when 8 > move CORRESPONDING source to target-8 > move target-8 to target > when other > move source to target > end-evaluate > > While both of these will do the job, there's a fair bit of writing and > they > leave me feeling "uncomfortable"... It SHOULD be possible to simply strip > out the slashes... > > As far as I can ascertain the INSPECT ...TRANSFORMING feature which Robert > recently reminded us about, will not transform something to nothing... > > Both of the above will work correctly whether the system is using > DD/MM/YYYY > or MM/DD/YYYY except that the names for day and month will be swapped, > another thing which, while not important in the context, leaves a bad > taste... > > Any thoughts? It makes me wonder how FUNCTION LENGTH(Target) could provide anything useful. Perhaps your code is to be COPYed into various programs where Target will be defined. [Pete] Yes. The code will be generated into programs where the target can vary. It WOULD be possible to pass the target length as another parameter, but I don't want to do that. The Function works fine. [/Pete] Or perhaps Target could be 'DEPENDING ON', or will it be done with ANY LENGTH ? [Pete] I think ANY LENGTH is only available with COBOL 2002 (?) Anyway, I don't use it. [/Pete] when 6 move source (1:2) to target move source (4:2) to target move source (7:2) to target Granted it is easy to forget the (3:2) etc on Target, how about: evaluate when 6 string source(1:2) source(4:2) source(7:2) into target when 8 string source(1:2) source(4:2) source(5:4) into target when other move source(1:10) to target end-evaluate Yes, that could work, but string doesn't offer any real advantage over move in this case, and some implementations may have more overhead on string than simple moves. Both constructs will be resolved at compile time so there's little to choose. Pete. -- "I used to write COBOL...now I can do anything." |
|
#9
| |||
| |||
| "Rick Smith" <ricksmith@mfi.net> wrote in message news:cI-dnSOaMrGogwHVnZ2dnUVZ_hCdnZ2d@posted.mid-floridainternet... > > "Pete Dashwood" <dashwood@removethis.enternet.co.nz> wrote in message > news:6g20pvFdnc3oU1@mid.individual.net... >> I have a situation in COBOL where I need to take a string and pull >> characters out of it. >> >> The characters I need to pull depend on the length of the target field, >> which could be 6, 8, or 10 bytes. >> >> The source string is 30 bytes. >> >> An example may help... >> >> SOURCE STRING: "19/03/2007 17:21:05:83" >> >> REQUIRED RESULTS: >> >> 6 byte = 190307 >> 8 byte = 19032007 >> 10 byte = 19/03/2007 >> >> Obviously, we can discount the 10 byte case as a simple move will do it. >> >> There are several immediately obvious approaches, but I was hoping for >> something smarter. > > [snip] > >> As far as I can ascertain the INSPECT ...TRANSFORMING feature which >> Robert >> recently reminded us about, will not transform something to nothing... > > [snip] > > -----Code using INSPECT ... CONVERTING > $set ans85 flag"ans85" flagas"s" > identification division. > program-id. A27B38. > data division. > working-storage section. > 01 source-string pic x(30) value "19/03/2007 17:21:05.83". > 01 source-pattern pic x(30) value "Dd/Mm\CcYy Hh:Nn;Ss.Tt". > 01 target-patterns. > 03 target-6 pic x(6) value "DdMmYy". > 03 target-8 pic x(8) value "DdMmCcYy". > 03 target-10 pic x(10) value "Dd/Mm\CcYy". > 01 target-length binary pic 9(4) value 0. > 01 target. > 03 pic x occurs 6 to 10 depending target-length. > procedure division. > begin. > perform 10 times > compute target-length = > (function integer (function random * 3)) * 2 + 6 > evaluate function length (target) > when 6 > move target-6 to target > when 8 > move target-8 to target > when 10 > move target-10 to target > end-evaluate > inspect target > converting source-pattern to source-string > display target > end-perform > stop run > . > -----Results > 19032007 > 19/03/2007 > 19032007 > 19/03/2007 > 19/03/2007 > 19/03/2007 > 190307 > 190307 > 190307 > 190307 > ----- > > Very impressive, Rick, and certainly a smart solution. (I think...:-)) I may have this wrong, but... as the value of the source string varies at run time, how would you get its value into a dynamic value clause? I think the CONVERTING needs to have a string to convert against, no? Rephrasing, will it still work if the contents of source-string are NOT known at compile time? I may have misunderstood the solution. (I did find it difficult to decipher.) Nevertheless, thanks for posting it. Pete. -- "I used to write COBOL...now I can do anything." |
|
#10
| |||
| |||
| In article <6g20pvFdnc3oU1@mid.individual.net>, Pete Dashwood <dashwood@removethis.enternet.co.nz> wrote: [snip] >SOURCE STRING: "19/03/2007 17:21:05:83" > >REQUIRED RESULTS: > >6 byte = 190307 >8 byte = 19032007 >10 byte = 19/03/2007 > >Obviously, we can discount the 10 byte case as a simple move will do it. > 12 mm pic xx. > 12 filler pic x. > 12 cc pic xx. > 12 yy pic xx. > 12 filler pic x(20). > >01 target-6. > 12 dd pic xx. > 12 mm pic xx. > 12 yy pic xx. > >01 target-8. > 12 dd pic xx. > 12 mm pic xx. > 12 cc pic xx. > 12 yy pic xx. >... > > move space to target > evaluate function LENGTH (target) > when 6 > move CORRESPONDING source to target-6 > move target-6 to target > when 8 > move CORRESPONDING source to target-8 > move target-8 to target > when other > move source to target > end-evaluate > > [snip]. 01 Date-Breakdown-Area. 05 dd pic xx. 05 f pic x. 05 mm pic xx 05 f pic xx 05 ccyy. 10 cc pic xx. 10 yy pic xx. .... Move Source-field(1:10) to Date-Breakdown-Area Evaluate FUNCTION LENGTH (target) When 10 Move Date-Breakdown-Area to target When 8 String dd, mm, ccyy delimited by size Into target When 6 String dd, mm, yy delimited by size Into target End-Evaluate This maintains documentation for 3:am fixes (something NEVER needed in The Future, I know) DD |
![]() |
| Thread Tools | |
| Display Modes | |
In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.