Felice
Felice

Reputation: 65

Finding a "whole string" within a string with Index (or another way)?

I am reading in a list of constituent attributes (tags) and only want to keep a subset of them. Here is how I attempted to do that.

DEFINE VARIABLE tagsToKeep AS CHARACTER. 
    
tagsToKeep = "Business Contact,Celebrate 2015,Celebrate 2017,Celebrate 2019,Certificate - Former Holder,Non-MB Church".

/*do a bunch of stuff to get the cRecord which is one tag, often with spaces or other characters in it.*/

IF INDEX(tagsToKeep, cRecord) > 0 THEN 
            DO:
                CREATE ttTag.
                    ASSIGN 
                      ttTag.tag-code = cRecord         
                      ttTag.constituent-id = ttAccount.pin-string.  
                      ttTag.tag-name = cRecord.
            END.

The trouble is that one of the tags is "Certificate" which is a substring of one of the strings in the TagsToKeep string -- "Certificate - Former Holder" gets included and created as a Tag. I only want to match on the strings that are essentially "whole word only".

Is there a (better) way to do what I want to do?

Upvotes: 1

Views: 520

Answers (2)

Mike Fechner
Mike Fechner

Reputation: 7192

In your code, use LOOKUP instead of INDEX

IF LOOKUP (cRecord, tagsToKeep) > 0 THEN 

(comma-) delimited lists are a key concept in the ABL.

Upvotes: 4

nwahmaet
nwahmaet

Reputation: 3909

If cRecord is a single tag, then it may be useful to take the list of to-keep tags and put those tags into a separate temp-table

DEFINE VARIABLE loop AS INTEGER NO-UNDO.
DEFINE VARIABLE cnt AS INTEGER NO-UNDO.

// Define and populate temp-table
DEFINE TEMP-TABLE ttTagsToKeep NO-UNDO
    FIELD TagName AS CHARACTER
    INDEX idx1 AS PRIMARY UNIQUE TagName.

cnt = NUM-ENTRIES(cTagsToKeep).
DO loop = 1 to cnt:
    CREATE ttTagsToKeep.
    ASSIGN ttTagsToKeep.TagName = TRIM(ENTRY(loop, cTagsToKeep)).
END.

Now you can look for a matching record. Whether you clean up cRecord or do multiple CAN-FINDs is up to you.

IF CAN-FIND(ttTagsToKeep WHERE ttTagsToKeep.TagName EQ cRecord) THEN
DO:
    // create ttTag record
END.

Upvotes: 3

Related Questions