I write a lot of meta-programs. Scripts in Ruby and PHP that generate different other programming languages and structures. Example say a script that reads from CSV and generates SQL queries.
Here is a problem I have encountered many times and I always get irritated with the way I solve it.
I have an array say
array= %w[chris sandy jones amar kuthrapalli]
I need to convert this array into a string that looks like
chris,sandy,jones,amar,kuthrapalli
Now its simple to iterate and add each name to a string. But I hate to write a special condition for not putting that "last extra comma" which will other wise go there.
The beauty of "foreach" lieks loop is that we dont have to bother about indices any more but then I am forced to use the iterator with indices. (for i =0 ...blah blah blah )
May be this is the only way to solve this problem but is there a better way ?
This is a simple problem but this kind of problem manifests itself is many different ways in the work I do.