2

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  heidi@gmail.com,hh@yahoo.com
12  tom@osu.edu;TQ@gmail.com
13  lisa@yahoo.com
14  linda@me.com;llewis@gmail.com,lvlv@yahoo.com 

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 ",").
Felice
  • 61
  • 5

2 Answers2

5

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 "linda@me.com;llewis@gmail.com,lvlv@yahoo.com".
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.
TheDrooper
  • 1,182
  • 1
  • 7
  • 14
1

You can't in a single statement.

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

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

For your pin 14 you will need to loop through the string, and count the entries manually.

define variable pin as character.
define variable pos as integer.
define variable pos2 as integer.
define variable cnt as integer.
define variable addr as character.
define variable startAt as integer.

pin = 'linda@me.com;llewis@gmail.com,lvlv@yahoo.com'.
startAt = 1.
pos = index(pin, ',', startAt).
do while pos gt 0:
    addr = substring(pin, startAt, pos - 1).
    pos2 = index(addr, ';').
    if pos2 gt pos then
       cnt = cnt + 1.
    else
        cnt = cnt + num-entries(addr, ';').
    
    startAt = pos + 1.
    pos = index(pin, ',', startAt).
end.

// after the last ,
if not trim(substring(pin, StartAt)) eq '' then
    cnt = cnt + 1.

message 
pin skip
cnt
view-as alert-box.

Just a gotcha, a string with a space will return 1 . An empty string will return 0.

message 
'empty: ' num-entries('') skip // 0
'space: ' num-entries(' ')     // 1
view-as alert-box.
nwahmaet
  • 3,589
  • 3
  • 28
  • 35
  • That makes send to add them up. Thanks. But then my next few statements would be getting the entry to assign to one of the 3 email address fields available, and I'm back where I started with the semi-colons. If you have a way to approach this would love to learn it. `IF iEmailCount GT 0 THEN ttAccount.emailaddress1 = ENTRY (1, ttConstituent.email, ";"). IF iEmailCount GT 1 THEN ttAccount.emailaddress2 = ENTRY (2, ttConstituent.email, ";").` – Felice Jun 24 '22 at 19:33
  • Sorry for the messiness of the comment. with everything running together. I am trying to follow the mini-markdown formatting but I haven't gotten it to work well. – Felice Jun 24 '22 at 19:38