Search code examples
openedgeprogress-4gl

Using num-entries() with more than one possible delimitor


I have a list of email addresses that I am reading in from a csv file, that are sometimes separated by commas and sometimes by semicolons when the person has more than 1 email address.

Examples:

Pin email_address
11  [email protected],[email protected]
12  [email protected];[email protected]
13  [email protected]
14  [email protected];[email protected],[email protected] 

Let's say I am reading these into a variable and counting them per person with another variable:

DEFINE VARIABLE emailString AS CHARACTER NO-UNDO.
DEFINE VARIABLE iEmailCount AS INTEGER.

I want to count the number of emails for the person. I know that I can use this syntax to count the entries between semi-colons, and between commas respectively.

iEmailCount = NUM-ENTRIES (emailString, ";").
iEmailCount = NUM-ENTRIES (emailString).

But how can I best say...

iEmailCount = Num_ENTRIES(emailString,";" OR ",").

Solution

  • You have to break the line down by each delimiter. Loop through one delimiter, then the second:

    DEFINE VARIABLE emailString AS CHARACTER NO-UNDO.
    DEFINE VARIABLE iEmailCount AS INTEGER NO-UNDO.
    DEFINE VARIABLE iLoop AS INTEGER NO-UNDO.
    
    iEmailCount = NUM-ENTRIES(emailString).
    DO iLoop = 1 TO NUM-ENTRIES(emailString):  /* Loop through comma delimiters */
        iEmailCount = iEmailCount + NUM-ENTRIES(ENTRY(iLoop, emailString), ";") - 1.  /* Sum by semicolon delimiter */
    END.
    

    If you're sure that a semicolon will only appear as a delimiter in the data, you can replace them with commas. Then it's a simple count of the number of comma entries:

    DEFINE VARIABLE emailString AS CHARACTER NO-UNDO INITIAL "[email protected];[email protected],[email protected]".
    DEFINE VARIABLE iEmailCount AS INTEGER NO-UNDO.
    
    emailString = REPLACE(emailString, ";", ",").
    iEmailCount = NUM-ENTRIES(emailString).
    
    MESSAGE "Email string: " emailString SKIP 
            "Count: " iEmailCount VIEW-AS ALERT-BOX.