{"id":53,"date":"2008-07-02T18:11:30","date_gmt":"2008-07-02T23:11:30","guid":{"rendered":"http:\/\/www.timrosenblatt.com\/blog\/2008\/07\/02\/comma-separated-values\/"},"modified":"2008-07-02T18:12:54","modified_gmt":"2008-07-02T23:12:54","slug":"comma-separated-values","status":"publish","type":"post","link":"http:\/\/www.timrosenblatt.com\/blog\/2008\/07\/02\/comma-separated-values\/","title":{"rendered":"Comma, Separated, Values,"},"content":{"rendered":"<p>I recently realized how much of the art of programming is just lots of little tricks that you pick up along the way. I&#8217;m resolving to share these little tricks, in the hopes that others will do the same, either on their blogs or in the comments here.<\/p>\n<p>Here&#8217;s one involving an array and building a string of comma separated values.<\/p>\n<p>Let us pretend you have an array called Tags. It contains the tag names as strings.<\/p>\n<pre>\r\nTags[0] = \"awesome\"\r\nTags[1] = \"groovy\"\r\nTags[2] = \"existential\"<\/pre>\n<p>You want to display these as CSV<\/p>\n<blockquote><p>awesome, groovy, existential<\/p><\/blockquote>\n<p>How do you do it? You might think &#8220;well, I want to print a tag, then a comma, then the next tag, and so on until I&#8217;m done&#8221;<\/p>\n<pre>for(i = 0; i &lt; Tags.length; i++) {\r\n    print Tags[i] + ', '\r\n}<\/pre>\n<p>You probably see what&#8217;s wrong already. The above code prints<\/p>\n<blockquote><p>awesome, groovy, existential,<\/p><\/blockquote>\n<p>OK. Maybe you could get clever:<\/p>\n<pre>\r\nfirst = true;for(i = 0; i &lt; Tags.length; i++) {\r\n    if(!first) { print ', ' }\r\n    print Tags[i]\r\n}<\/pre>\n<p>or<\/p>\n<pre>for(i = 0; i &lt; Tags.length; i++) {\r\n    if(i == Tags.length - 1) { print ', ' }\r\n    print Tags[i]\r\n}<\/pre>\n<p>But ouch. Don&#8217;t do that.<\/p>\n<p>Many languages provide a built in function for converting arrays into CSV strings. PHP calls it implode(), Javascript calls it Array.join(). Simply put, it returns a string consisting of the values of the array, with a delimiter in between the values.<\/p>\n<pre>Tag_string = Tags.join(', ')<\/pre>\n<p>Done.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I recently realized how much of the art of programming is just lots of little tricks that you pick up along the way. I&#8217;m resolving to share these little tricks, in the hopes that others will do the same, either on their blogs or in the comments here. Here&#8217;s one involving an array and building &hellip; <a href=\"http:\/\/www.timrosenblatt.com\/blog\/2008\/07\/02\/comma-separated-values\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Comma, Separated, Values,&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[88],"tags":[148,580,147,3,149,581],"_links":{"self":[{"href":"http:\/\/www.timrosenblatt.com\/blog\/wp-json\/wp\/v2\/posts\/53"}],"collection":[{"href":"http:\/\/www.timrosenblatt.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.timrosenblatt.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.timrosenblatt.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.timrosenblatt.com\/blog\/wp-json\/wp\/v2\/comments?post=53"}],"version-history":[{"count":0,"href":"http:\/\/www.timrosenblatt.com\/blog\/wp-json\/wp\/v2\/posts\/53\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.timrosenblatt.com\/blog\/wp-json\/wp\/v2\/media?parent=53"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.timrosenblatt.com\/blog\/wp-json\/wp\/v2\/categories?post=53"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.timrosenblatt.com\/blog\/wp-json\/wp\/v2\/tags?post=53"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}