This code
disp entry(5,"1 2 3 4"," ").
Returns an error because entry 5 is outside the range.
How do I count the number of delimiters in the string so that I don't attempt accessing an entry that doesn't exist?
This code
disp entry(5,"1 2 3 4"," ").
Returns an error because entry 5 is outside the range.
How do I count the number of delimiters in the string so that I don't attempt accessing an entry that doesn't exist?
Alternatively, if you want to get the nth entry in a string or nothing at all if that entry doesn't exist (without an error), you can append "n" delimiters to the string before you check it. This will ensure that the statement completes properly - you'll have to decide what to do when you get a blank entry back.
DEFINE VARIABLE cString AS CHARACTER NO-UNDO.
DEFINE VARIABLE iEntry AS INTEGER NO-UNDO.
cString = 'jim bob tom ed frank william'.
iEntry = 23.
DISPLAY ENTRY(iEntry,cString + FILL(' ',iEntry),' ').
Of course, rather than returning a blank entry, you can also return a known error value, as shown in the alternative code below (which will return "INVALID" for all entries that fall outside the original list). Note the leading delimiter (in this case a space) on my known error value - this is important to ensure that each known error value gets appended to the string as a separate list item.
DISPLAY ENTRY(iEntry,cString + FILL(' INVALID',iEntry),' ').
I like to use this approach when dealing with user-entered data, or data coming from external sources that should be formatted correctly...but sometimes might not be.