Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 5 of 5 total
Thread Best practice on list building
Mon, Jan 23 2006 8:16 AMPermanent Link

I'm working on new code to generate SQL statements. I have code which will
loop around an array and build a list of fields, each comma separated.
Thus it may generate something like
SELECT FIELD1, FIELD2, FIELD3, FIELD4 From MyTable

Now, what I'd like to know is what is good practice to handle the last
comma. Obviously the last field can't have a comma after it. At the moment
I build up my SQL in a TStringList and have a function StripLastComma()
which takes the string list and looks at the last line and removes the
trailing comma. I call this at the end of any listing which outputs the
fields followed by a comma. Makes it easy to do. But I wonder if this is
the best option? In the past I've also added a comma based on whether this
is the last item in the list, but I worry that I may go editing and the
commas will break, and it increases the code run per field.

What do other people do? What works reliably, and is efficient?

/Matthew Jones/
Mon, Jan 23 2006 8:59 AMPermanent Link

Roy Lambert

NLH Associates

Team Elevate Team Elevate

Matthew


I used to do it like you (some of my stuff still does) but these days I generally build each clause separately just using a string and then combine. For the select fields I add a comma after each and then use Delete to get rid of the last character (I know its a comma).

The other way you can look at is TStringList.CommaText


Roy Lambert
Mon, Jan 23 2006 9:24 AMPermanent Link

"Robert"

"Matthew Jones" <matthew@matthewdelme-jones.delme.com> wrote in message
news:memo.20060123131653.1408C@nothanks.nothanks.co.uk...
> I'm working on new code to generate SQL statements. I have code which will
> loop around an array and build a list of fields, each comma separated.

You can put the comma at the beginning, instead of at the end of the loop,
and skip the first field.

If OutputString <> '' then OutputString := OutputString + ', ';
... make field
OutputString := OutputString + Field;

Robert

Mon, Jan 23 2006 12:04 PMPermanent Link

I've done that in the past, but with a SELECT at the front it doesn't
help. Good thought though. I've used a boolean that way (bInsertComma) and
that seems cleaner to me in a self-documenting sort of a way.

/Matthew Jones/
Tue, Jan 24 2006 4:48 AMPermanent Link

I'm also reminded I used to use a function to append a string that would
take a parameter "bLastItem" to which I'd pass "nLoop <> List.Count".
Plenty of ways, but nothing so outstandingly efficient as to be a killer.

/Matthew Jones/
Image