Break String Variable Into Array of Words

 

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: 2467027419e6cdfe0ca21a4c9dba5623].  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 
String>Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi Hi ... Hi Hi<
Return code: +000000000
Count: +0267
Item #+0001 Length: 000000002: Hi
Item #+0002 Length: 000000002: Hi
Item #+0003 Length: 000000002: Hi
Item #+0004 Length: 000000002: Hi
Item #+0005 Length: 000000002: Hi
...
Item #+0265 Length: 000000002: Hi
Item #+0266 Length: 000000002: Hi
Item #+0267 Length: 000000002: Hi
String>Bye Bye Bye Bye Bye Bye Bye Bye Bye Bye Bye Bye Bye Bye Bye ... Bye Bye<
Return code: +000000008
Count: +0400
Item #+0001 Length: 000000003: Bye
Item #+0002 Length: 000000003: Bye
Item #+0003 Length: 000000003: Bye
Item #+0004 Length: 000000003: Bye
Item #+0005 Length: 000000003: 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).


This page was last updated on September 20, 2019.