2

newbie question here.

I need to create a list. but my problem is what is the best way to not start with a comma?

eg:

output to /usr2/appsrv/test/Test.txt.
def var dTextList as char.
for each emp no-lock:
  dTextList = dTextList + ", " + emp.Name.
end.
put unformatted dTextList skip.
output close.

then my end result is

, jack, joe, brad

what is the best way to get rid of the leading comma?

thank you

Tom Bascom
  • 13,405
  • 2
  • 27
  • 33
BobNoobGuy
  • 1,551
  • 2
  • 30
  • 62

6 Answers6

1

Here's one way:

ASSIGN 
   dTextList = dTextList + ", " WHEN dTextList > ""
   dTextList = dTextList + emp.Name 
   .
Tim Kuehn
  • 3,201
  • 1
  • 17
  • 23
1

This does it without any conditional logic:

for each emp no-lock:
  csv = csv + emp.Name + ",".
end.
right-trim( csv, "," ).

or you can do this:

for each emp no-lock:
  csv = substitute( "&1,&2" csv, emp.Name ).
end.
trim( csv, "," ).

Which also has the advantage of playing nicely with unknown values (the ? value...)

TRIM() trims both sides, LEFT-TRIM() only does leading characters and RIGHT-TRIM() gets trailing characters.

Tom Bascom
  • 13,405
  • 2
  • 27
  • 33
1

My vanilla list:

output to /usr2/appsrv/test/Test.txt.
def var dTextList as char no-undo.
for each emp no-lock:
  dTextList = substitute( "&1, &2", dTextList, emp.Name )
end.
put unformatted substring( dTextList, 3 ) skip.
output close.
  1. substitute prevents unknowns from wiping out list
  2. keep list delimiter checking outside of loop
  3. generally leave the list delimiter prefixed unless the prefix really needs to go as in the case when outputting it

When using delimited lists often you may want to consider a creating a list class to remove this irrelevant noise out of your code so that you can functionally just add an item to a list and export a list without tinkering with these details every time.

Stefan Drissen
  • 3,266
  • 1
  • 13
  • 21
0

I usually do

ASSIGN dTextList = dTextList + (if dTextList = '' then '' else ',') + emp.name.
bupereira
  • 1,463
  • 8
  • 13
0

I come up (well my colleague did) he come up with this:

dTextList = substitute ("&1&3&2", dTextList, emp.Name, min(dTextList,",")).

But it is cool to see various ways to do this. Thank you for all the response

BobNoobGuy
  • 1,551
  • 2
  • 30
  • 62
0

This results in no leading comma (delimiter) and no fiddling with trim/substring/etc

def var cDelim as char.
def var dTextList as char.
cDelim = ''.
for each emp no-lock:
  dTextList = dTextList + cDelim + emp.Name.
   cDelim = ','.
end.
nwahmaet
  • 3,589
  • 3
  • 28
  • 35