I'm still finding additional functions I need as I continue to work through the HTML objects I am incorporating into a project. Although languages like REXX have built-in functions to parse strings into words, there isn't a similar intrinsic function in GnuCOBOL, so I put this one together. As presented here, it will handle a table of up to 400 words of 25 characters each. An error code of +8 will be returned if the number of words in the string exceeds 400. Even if the string exceeds 400 words, it will return the 400 extracted before the limit is exceeded. I have not made provision for encountering a word exceeding 25 characters, and if that occurs, the word will simply be truncated without warning. However, that would be a trivial modification to check word length before storing in the table.
To install the source programs (the subprogram and a test program), and compile them, execute the bash script: downloads/stringWords.setup [md5: 560a464a6fcb87f31d7b4a2c63bfe176]. Right click and save the script in the location where you want the source program to reside, then execute it. It will create two files containing the source programs, then execute the GnuCOBOL compiler to compile them into the run unit for the test program. The GnuCOBOL compiler must be installed prior to executing the bash script.
Output from the test/verification program:
jay@Phoenix ~ $ ./testSubs Expect a return code of +4, because first word length exceeds 25 characters. String: Oooooooooooooooooooooooops Hi Hi Hi Hi Hi Hi ... Hi Return code: +000000004 Count: +0268 Item #+0001 Length: +0026: Oooooooooooooooooooooooop Item #+0002 Length: +0002: Hi Item #+0003 Length: +0002: Hi Item #+0004 Length: +0002: Hi Item #+0005 Length: +0002: Hi ... Item #+0265 Length: +0002: Hi Item #+0266 Length: +0002: Hi Item #+0267 Length: +0002: Hi Item #+0268 Length: +0002: Hi Expect a return code of +8, because number of words exceeds table size of 400 words. String: Bye Bye Bye Bye Bye Bye Bye Bye Bye Bye Bye Bye Bye ... Bye Return code: +000000008 Count: +0400 Item #+0001 Length: +0003: Bye Item #+0002 Length: +0003: Bye Item #+0003 Length: +0003: Bye Item #+0004 Length: +0003: Bye Item #+0005 Length: +0003: Bye ... Item #+0398 Length: 000000003: Bye Item #+0399 Length: 000000003: Bye Item #+0400 Length: 000000003: Bye
If you want to be able to dynamically call the subprogram move the object module (stringWords.so) to a location included in your COB_LIBRARY_PATH.
* Updated September 20, 2019 * I was tired of looking for and copying the Working-Storage fields for this, and a few other, routines, so I updated the installation script to provide a copybook that may be used in the calling program for this routine. For easiest use, you should copy the copybook file into the directory that GnuCOBOL searches during compile time for copy books (on my Linux system that is /usr/local/share/gnucobol/copy).
* Updated November 8, 2019 * I needed to call this routine from another subroutine, which was also building a table of words, so I thought it would be more reasonable to save the word length in the table returned from this routine. So I modified this routine to store the word length for each word. Also added code to ensure that the storage provided by the caller for the table is equal to the storage defined in this routine. That has not ever been an issue with this particular routine, but I have run into it recently with another routine, so have been adding it as a standard check in everything I have been writing.
This page was last updated on November 08, 2019.