# Programmers - What method do you use to display comma seperated lists correctly?

**URL:** <https://boards.straightdope.com/t/programmers-what-method-do-you-use-to-display-comma-seperated-lists-correctly/580604>\
**Category:** Factual Questions\
**Created:** [May 3, 2011, 7:10pm UTC](https://boards.straightdope.com/t/programmers-what-method-do-you-use-to-display-comma-seperated-lists-correctly/580604 "2011-05-03T19:10:29Z")\
**Posts on this page:** 19\
**Page:** 3

<div class="post-metadata">

**Author:** ![enipla](https://avatars.discourse-cdn.com/v4/letter/e/54ee81/32.png) [@enipla](https://boards.straightdope.com/u/enipla)\
**Post date:** [May 4, 2011, 11:44pm UTC](https://boards.straightdope.com/t/programmers-what-method-do-you-use-to-display-comma-seperated-lists-correctly/580604/41 "2011-05-04T23:44:00Z")

</div>

Sort of related. Why would any one use a comma as a delimeter? Pipe people, pipe.

---

<div class="post-metadata">

**Author:** ![Jragon](https://avatars.discourse-cdn.com/v4/letter/j/e19b73/32.png) [@Jragon](https://boards.straightdope.com/u/Jragon)\
**Post date:** [May 5, 2011, 2:06am UTC](https://boards.straightdope.com/t/programmers-what-method-do-you-use-to-display-comma-seperated-lists-correctly/580604/42 "2011-05-05T02:06:12Z")

</div>

> [@enipla](#):
>
> Sort of related. Why would any one use a comma as a delimeter? Pipe people, pipe.

Depending on the font, pipes can be confusing. I also find pipes make it hard to read things unless combined with a leading and trailing space " | ".

---

<div class="post-metadata">

**Author:** ![Capt.Ridley\_s\_Shooting\_Party](https://avatars.discourse-cdn.com/v4/letter/c/cc9497/32.png) [@Capt.Ridley\_s\_Shooting\_Party](https://boards.straightdope.com/u/Capt.Ridley_s_Shooting_Party)\
**Post date:** [May 5, 2011, 8:41am UTC](https://boards.straightdope.com/t/programmers-what-method-do-you-use-to-display-comma-seperated-lists-correctly/580604/43 "2011-05-05T08:41:51Z")

</div>

> [@Ximenean](#):
>
> Is this, by any chance, Haskell? 😉

There’s a good chance you’re correct. 😛

---

<div class="post-metadata">

**Author:** ![friedo](https://avatars.discourse-cdn.com/v4/letter/f/8edcca/32.png) [@friedo](https://boards.straightdope.com/u/friedo)\
**Post date:** [May 5, 2011, 10:43am UTC](https://boards.straightdope.com/t/programmers-what-method-do-you-use-to-display-comma-seperated-lists-correctly/580604/44 "2011-05-05T10:43:22Z")

</div>

> [@Ximenean](#):
>
> Is this, by any chance, Haskell? 😉

Of course it is. Only Haskell psychopaths would think “intercalate” is a better word than “join.” 🙂

---

<div class="post-metadata">

**Author:** ![Ximenean](https://avatars.discourse-cdn.com/v4/letter/x/aca169/32.png) [@Ximenean](https://boards.straightdope.com/u/Ximenean)\
**Post date:** [May 5, 2011, 10:46am UTC](https://boards.straightdope.com/t/programmers-what-method-do-you-use-to-display-comma-seperated-lists-correctly/580604/45 "2011-05-05T10:46:03Z")

</div>

Their campaign slogan “intercalate us” was a miserable failure, true.

---

<div class="post-metadata">

**Author:** ![Capt.Ridley\_s\_Shooting\_Party](https://avatars.discourse-cdn.com/v4/letter/c/cc9497/32.png) [@Capt.Ridley\_s\_Shooting\_Party](https://boards.straightdope.com/u/Capt.Ridley_s_Shooting_Party)\
**Post date:** [May 5, 2011, 11:03am UTC](https://boards.straightdope.com/t/programmers-what-method-do-you-use-to-display-comma-seperated-lists-correctly/580604/46 "2011-05-05T11:03:43Z")

</div>

> [@friedo](#):
>
> Of course it is. Only Haskell psychopaths would think “intercalate” is a better word than “join.” 🙂

😛

I admit I’d never heard “intercalate” used before I learned Haskell. Apparently it’s a term of art in molecular biology and in whatever -ology studies the Hebrew calendar. Join has another meaning in Haskell:

```auto

join :: Monad m => m (m a) -> m a

```

Example in the list monad:

```auto

join [[1, 2, 3], [4, 5, 6]]

```

Result:

```auto

[1,2,3,4,5,6]

```

i.e. collapses nested linked lists into a single linked list.

---

<div class="post-metadata">

**Author:** ![Folacin](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/folacin/32/3195_2.png) [@Folacin](https://boards.straightdope.com/u/Folacin)\
**Post date:** [May 5, 2011, 1:17pm UTC](https://boards.straightdope.com/t/programmers-what-method-do-you-use-to-display-comma-seperated-lists-correctly/580604/47 "2011-05-05T13:17:02Z")

</div>

> [@RaftPeople](#):
>
> **Lobsang** , don’t try to make the most code-compact method, just do it the simplest most straight forward manner possible. Having a condition on the first time through the loop to avoid adding a comma is perfectly ok.

> [@TriPolar](#):
>
> Comments can make it perfectly clear what is happening. In general, all reasonable methods will operate around the same level of efficiency, and doing this at all should not be a performance critical operation.  
> \<snipage\>  
> I’ll mention that while hacking and cursing my way through some horrible code today, I would not have been impressed to see an elegant solution for this trivial operation. There are far more important considerations in coding than this.

These two. If you’ve ever had to maintain someone else’s code, you will bless comments and curse code-compactness.

We used to apply a “that’s cool” test to code during walkthroughs - if that phrase was uttered, the code was rewritten to be clearer (or at least heavily commented).

I remember C being described as a “write-only” language (as opposed to COBOL being “read-only”, IIRC).

---

<div class="post-metadata">

**Author:** ![c\_goat](https://avatars.discourse-cdn.com/v4/letter/c/f4b2a3/32.png) [@c\_goat](https://boards.straightdope.com/u/c_goat)\
**Post date:** [May 6, 2011, 4:43pm UTC](https://boards.straightdope.com/t/programmers-what-method-do-you-use-to-display-comma-seperated-lists-correctly/580604/48 "2011-05-06T16:43:21Z")

</div>

Usually I only ever have to build a comma separated list for logging, so it would look something like:

```auto

if(numItems > 0)
{
   cout << item[0];
   for(i=1; i<numItems; ++i)
   {
      cout << ", " << item*;
   }
   cout << endl;
}

```

No need to check if you’re the first item every iteration of the loop, and no trailing comma to strip off. Yeah you’re duplicating code, and normally I hate doing that, but in this case I think it’s more readable as to what’s going on. The first item is treated differently. This also works for a single item list.

---

<div class="post-metadata">

**Author:** ![davidm](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/davidm/32/225_2.png) [@davidm](https://boards.straightdope.com/u/davidm)\
**Post date:** [May 6, 2011, 4:52pm UTC](https://boards.straightdope.com/t/programmers-what-method-do-you-use-to-display-comma-seperated-lists-correctly/580604/49 "2011-05-06T16:52:47Z")

</div>

> [@ZipperJJ](#):
>
> FWIW I just loop through and put everything into a string with my separator character(s) after, then trim the end of the resulting string of my character(s).

That’s how I usually do it. After you exit the loop trim off the trailing comma.

---

<div class="post-metadata">

**Author:** ![donkeypc2k11](https://avatars.discourse-cdn.com/v4/letter/d/b77776/32.png) [@donkeypc2k11](https://boards.straightdope.com/u/donkeypc2k11)\
**Post date:** [May 13, 2011, 2:50am UTC](https://boards.straightdope.com/t/programmers-what-method-do-you-use-to-display-comma-seperated-lists-correctly/580604/50 "2011-05-13T02:50:47Z")

</div>

It is very important to filter the raw content of data block by replacing non-ALPHA NUMERIC like “,” the comma with a blankor space. The Data field will then be accurate. Create output files outputs: Number of Records should match especially based on index field. Fieilds with expected content like why blank not a zero value. People’s name with numbers. **Unless you have a fixed data block size you will problems with commas in dollar amounts.**

---

<div class="post-metadata">

**Author:** ![davidm](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/davidm/32/225_2.png) [@davidm](https://boards.straightdope.com/u/davidm)\
**Post date:** [May 13, 2011, 2:59am UTC](https://boards.straightdope.com/t/programmers-what-method-do-you-use-to-display-comma-seperated-lists-correctly/580604/51 "2011-05-13T02:59:53Z")

</div>

That depends on how you’re representing dollar amounts. If you’re storing them as numbers, rather than strings, they you shouldn’t even be using commas in dollar amounts. You should let the display interface put in things like commas and dollar signs. If you do want to store them as strings with commas for some reason, or if you have commas in other text, you can quote the entire string or use an escape character before the commas.

---

<div class="post-metadata">

**Author:** ![donkeypc2k11](https://avatars.discourse-cdn.com/v4/letter/d/b77776/32.png) [@donkeypc2k11](https://boards.straightdope.com/u/donkeypc2k11)\
**Post date:** [May 13, 2011, 3:07am UTC](https://boards.straightdope.com/t/programmers-what-method-do-you-use-to-display-comma-seperated-lists-correctly/580604/52 "2011-05-13T03:07:56Z")

</div>

If this is a one shot assignment… I would use a freeware EDXOR.  
Very fast like DOS [http://members.ozemail.com.au/~nulifetv/freezip/freeware/](http://members.ozemail.com.au/~nulifetv/freezip/freeware/)  
**To use** : Select ALL then FORMAT then WIPE -\>NON-ASCII plus others.  
Do not select WORD WRAP on EDIT option (if do not see full width. Use the bottom bar-- it has no limit!!)  
Save it as text by adding “.txt”. Open this file from Ms Excel file import. The conversion is flawless…

---

<div class="post-metadata">

**Author:** ![HMS\_Irruncible](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/hms_irruncible/32/7394_2.png) [@HMS\_Irruncible](https://boards.straightdope.com/u/HMS_Irruncible)\
**Post date:** [May 14, 2011, 2:10am UTC](https://boards.straightdope.com/t/programmers-what-method-do-you-use-to-display-comma-seperated-lists-correctly/580604/53 "2011-05-14T02:10:05Z")

</div>

> [@TriPolar](#):
>
> Your point is correct. By elegant solution I was referring to an optimized loop of some sort. Of course there is an even better solution for being expressive with whatever method is used. It’s called a comment.
> 
> ```auto
> 
> //make comma separated list out of items
> 
> ```

Comments are biodegradable and eventually decompose into lies. They are for explaining stupid things that you have no control over, not for explaining code that ought to be self-explanatory.

---

<div class="post-metadata">

**Author:** ![friedo](https://avatars.discourse-cdn.com/v4/letter/f/8edcca/32.png) [@friedo](https://boards.straightdope.com/u/friedo)\
**Post date:** [May 14, 2011, 3:18am UTC](https://boards.straightdope.com/t/programmers-what-method-do-you-use-to-display-comma-seperated-lists-correctly/580604/54 "2011-05-14T03:18:33Z")

</div>

> [@cosmic relief](#):
>
> comments are biodegradable and eventually decompose into lies. They are for explaining stupid things that you have no control over, not for explaining code that ought to be self-explanatory.

i \<3 u.

---

<div class="post-metadata">

**Author:** ![TriPolar](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/tripolar/32/3008_2.png) [@TriPolar](https://boards.straightdope.com/u/TriPolar)\
**Post date:** [May 14, 2011, 4:17am UTC](https://boards.straightdope.com/t/programmers-what-method-do-you-use-to-display-comma-seperated-lists-correctly/580604/55 "2011-05-14T04:17:11Z")

</div>

> [@Cosmic Relief](#):
>
> Comments are biodegradable and eventually decompose into lies. They are for explaining stupid things that you have no control over, not for explaining code that ought to be self-explanatory.

Code is crap. Comments are the place to clearly explain an algorithm. A properly commented block of code can be re-written in less time than it takes to identify and fix a bug in it. Some of the worst problems come from assuming that the code represents the way something is supposed to work, causing an original error to propogate long after it is created. If the comments fail to reflect changes to code over time, then programmers are doing their jobs, and usually their managers as well.

---

<div class="post-metadata">

**Author:** ![Hambil](https://avatars.discourse-cdn.com/v4/letter/h/3be4f8/32.png) [@Hambil](https://boards.straightdope.com/u/Hambil)\
**Post date:** [May 14, 2011, 4:47am UTC](https://boards.straightdope.com/t/programmers-what-method-do-you-use-to-display-comma-seperated-lists-correctly/580604/56 "2011-05-14T04:47:28Z")

</div>

A number of the example above use a constant value like -1, as a general rule any constant in code should be seen as a red flag. If you find yourself using a constant, take a step back and you can usually find a better way to do it.

Constants are not always bad, though I would strongly argue in THIS case where you are doing a -1 or +1 or such they are pretty much always bad.

---

<div class="post-metadata">

**Author:** ![Hambil](https://avatars.discourse-cdn.com/v4/letter/h/3be4f8/32.png) [@Hambil](https://boards.straightdope.com/u/Hambil)\
**Post date:** [May 14, 2011, 4:49am UTC](https://boards.straightdope.com/t/programmers-what-method-do-you-use-to-display-comma-seperated-lists-correctly/580604/57 "2011-05-14T04:49:15Z")

</div>

> [@TriPolar](#):
>
> Code is crap. Comments are the place to clearly explain an algorithm. A properly commented block of code can be re-written in less time than it takes to identify and fix a bug in it. Some of the worst problems come from assuming that the code represents the way something is supposed to work, causing an original error to propogate long after it is created. If the comments fail to reflect changes to code over time, then programmers are doing their jobs, and usually their managers as well.

If I can’t read and understand your code as easily as I could read a book, without comments, I would say you need to work on your coding skills. I love comments, use them a lot, but, never, ever, in place of readable code.

---

<div class="post-metadata">

**Author:** ![TriPolar](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/tripolar/32/3008_2.png) [@TriPolar](https://boards.straightdope.com/u/TriPolar)\
**Post date:** [May 14, 2011, 5:09am UTC](https://boards.straightdope.com/t/programmers-what-method-do-you-use-to-display-comma-seperated-lists-correctly/580604/58 "2011-05-14T05:09:36Z")

</div>

> [@Hambil](#):
>
> If I can’t read and understand your code as easily as I could read a book, without comments, I would say you need to work on your coding skills. I love comments, use them a lot, but, never, ever, in place of readable code.

I agree code should be clear and readable. But it is too terse to incorporate every detail to take into consideration. This problem is bad enough when a variable is used which may have last been set far from the current line of code, and the problem can become a disaster with polymorphic objects. The class of the object is obscure from it’s reference, and comments are necessary to convey it’s potential content.

---

<div class="post-metadata">

**Author:** ![Hambil](https://avatars.discourse-cdn.com/v4/letter/h/3be4f8/32.png) [@Hambil](https://boards.straightdope.com/u/Hambil)\
**Post date:** [May 14, 2011, 5:16am UTC](https://boards.straightdope.com/t/programmers-what-method-do-you-use-to-display-comma-seperated-lists-correctly/580604/59 "2011-05-14T05:16:36Z")

</div>

> [@TriPolar](#):
>
> I agree code should be clear and readable. But it is too terse to incorporate every detail to take into consideration. This problem is bad enough when a variable is used which may have last been set far from the current line of code, and the problem can become a disaster with polymorphic objects. The class of the object is obscure from it’s reference, and comments are necessary to convey it’s potential content.

What you are describing is what I often refer to as ‘code hiding’. It can happen for many reasons but the most common is people feeling too strong a need to remove repetitive code to a function. This need (taught to them with good intentions) can lead to hiding structure, and structure should never be hidden even if it is repetitive.

Object oriented programming can really amplify this problem.

A (simplified) example. There is an object called car. Someone decides that instead of calling Car.start then Car.move then Car.stop, they will just create a method called Car.do.

Now the next programmer reading the code has to dig an extra layer deep to see what the heck the Car object is actually doing.

Add some more code hiding (other objects usually) within the Car object and now the poor programmer is digging three levels deep to find out what should have been obvious from a quick glance at the first level of code to begin with.

[Previous page](https://boards.straightdope.com/t/programmers-what-method-do-you-use-to-display-comma-seperated-lists-correctly/580604.md?page=2)
