BobNoobGuy
BobNoobGuy

Reputation: 1645

how to create comma separated value in progress openEdge

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

Upvotes: 2

Views: 1385

Answers (6)

nwahmaet
nwahmaet

Reputation: 3909

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.

Upvotes: 0

Stefan Drissen
Stefan Drissen

Reputation: 3379

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.

Upvotes: 1

BobNoobGuy
BobNoobGuy

Reputation: 1645

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

Upvotes: 0

Tom Bascom
Tom Bascom

Reputation: 14020

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.

Upvotes: 1

Tim Kuehn
Tim Kuehn

Reputation: 3251

Here's one way:

ASSIGN 
   dTextList = dTextList + ", " WHEN dTextList > ""
   dTextList = dTextList + emp.Name 
   .

Upvotes: 1

bupereira
bupereira

Reputation: 1498

I usually do

ASSIGN dTextList = dTextList + (if dTextList = '' then '' else ',') + emp.name.

Upvotes: 0

Related Questions