Thursday, July 19, 2007

ListToArray in ColdFusion 8

There has been lots and lots of discussion in CF blogs and forums about ListToArray not supporting empty elements. Seeing which I had blogged about a simple way which could do the same. Thanks to Charlie Griefer for pointing out the problem in it and then thanks to Ben Nadel and Andrew Clark for pointing me in the right direction.

Though it got pretty late, I was able to sneak-in this change in ColdFusion 8. ListToArray() now takes an additional optional argument "includeEmptyElements", which if 'true' will include the empty elements of list into the array. Default is of course 'false'. It also takes care of empty elements at the end of list and multiple delimiters. Here is how the function looks

ListToArray(list, delimiter, includeEmptyElements) returns Array

Lets take a look at couple of examples to see it working

<cfset list = "a,b,,c, ,d,,">
<cfset arr = ListToArray(list, ',', true)>
<cfdump var="#arr#">
Here is how the output looks.








Here is another example.

<cfset list = "one,/$/,six">
<cfset arr = listToArray(list, ",$/",true)>
<cfdump var="#arr#">

The output for which looks like this







Though we wanted to, there was just not enough time to make similar change in all the list functions for CF 8. Something for CF 9 :-)

15 comments:

Sam Farmer said...

Cool. Nice addition. Glad you left something for CF9! ;)

Anonymous said...

There won't be a CF9!
Everyone will die soon! :)

gary gilbert said...

Great addition to CF8. That has been something that I've wanted in CF for ages!

Rupesh Kumar said...

Thanks Sam and Gary. Didn't we say that we listen to our users :-)

Giancarlo Gomez said...

Rupesh,

I tried this today on my box and got different results and can not figure our why. I used both the new listToArray in CF8 and the list2Array function from one of your previous post. The problem is that my delimiter is "," (all three are the total delimiter). The list2array returns the correct amount of elements but CF8's listToArray returns 24 elements when there should only be 8. Below is the code I tested.


<cfscript>
// Empty List Function
function list2Array(list)
{
var i = 0;
var retlist = ArrayNew(1);
var arr = list.split('","',-1);
for(i = 1; i <= ArrayLen(arr); i++)
ArrayAppend(retList,arr[i]);
return retList;
}
theList='"one","two","three","","four","","","seven"';
theArray1=listToArray(theList,'","',true);
theArray2=list2Array(theList);
</cfscript>
<cfdump var="#theArray1#" />
<cfdump var="#theArray2#" />

Rupesh Kumar said...

Giancarlo,
In ColdFusion ListToArray function, when you specify multi-character delimiter, each character is treated as a separate delimiter and not the whole string. This behavior exists since ListToArray function was introduced. So when you had a delimiter '","', it treated '"' and ',' as separate delimiters and hence 24 elements. If you want to have a multi-character delimiter, as in your example, you should use the UDF List2Array I had mentioned.

halhelms said...

Rupesh,

Unfortunately, if you use the new syntax for CF, the empty elements are not ignored when looping over the list.

<cfset a = {2,,4,5} />
<cfloop array="#a" index="anElement">
#anElement#
<cfloop>

This will break

Rupesh Kumar said...

Hal,
I didn't really understand what breaks.
New syntax for array does not even allow you to have empty elements in array.
So the following code will not run..
<cfset a = [2,4, ,5] />
<cfloop array="#a#" index="anElement">
<cfoutput>#anElement#</cfoutput>
</cfloop>

Tom Mollerus said...

@Hal, aren't you using the syntax to create a structure? That may be why your loop isn't working.

Anonymous said...

Plus it would break anyways because his variable is set to #a not #a#

cvxv said...

看房子,買房子,建商自售,自售,台北新成屋,台北豪宅,新成屋,豪宅,美髮儀器,美髮,儀器,髮型,EMBA,MBA,學位,EMBA,專業認證,認證課程,博士學位,DBA,PHD,在職進修,碩士學位,推廣教育,DBA,進修課程,碩士學位,網路廣告,關鍵字廣告,關鍵字,課程介紹,學分班,文憑,牛樟芝,段木,牛樟菇,日式料理, 台北居酒屋,日本料理,結婚,婚宴場地,推車飲茶,港式點心,尾牙春酒,台北住宿,國內訂房,台北HOTEL,台北婚宴,飯店優惠,台北結婚,場地,住宿,訂房,HOTEL,飯店,造型系列,學位,SEO,婚宴,捷運,學區,美髮,儀器,髮型,看房子,買房子,建商自售,自售,房子,捷運,學區,台北新成屋,台北豪宅,新成屋,豪宅,學位,碩士學位,進修,在職進修, 課程,教育,學位,證照,mba,文憑,學分班,台北住宿,國內訂房,台北HOTEL,台北婚宴,飯店優惠,住宿,訂房,HOTEL,飯店,婚宴,台北住宿,國內訂房,台北HOTEL,台北婚宴,飯店優惠,住宿,訂房,HOTEL,飯店,婚宴,台北住宿,國內訂房,台北HOTEL,台北婚宴,飯店優惠,住宿,訂房,HOTEL,飯店,婚宴,結婚,婚宴場地,推車飲茶,港式點心,尾牙春酒,台北結婚,場地,結婚,場地,推車飲茶,港式點心,尾牙春酒,台北結婚,婚宴場地,結婚,婚宴場地,推車飲茶,港式點心,尾牙春酒,台北結婚,場地,居酒屋,燒烤,美髮,儀器,髮型,美髮,儀器,髮型,美髮,儀器,髮型,美髮,儀器,髮型,小套房,小套房,進修,在職進修,留學,證照,MBA,EMBA,留學,MBA,EMBA,留學,進修,在職進修,牛樟芝,段木,牛樟菇,關鍵字排名,網路行銷,PMP,在職專班,研究所在職專班,碩士在職專班,PMP,證照,在職專班,研究所在職專班,碩士在職專班,SEO,廣告,關鍵字,關鍵字排名,網路行銷,網頁設計,網站設計,網站排名,搜尋引擎,網路廣告,SEO,廣告,關鍵字,關鍵字排名,網路行銷,網頁設計,網站設計,網站排名,搜尋引擎,網路廣告,SEO,廣告,關鍵字,關鍵字排名,網路行銷,網頁設計,網站設計,網站排名,搜尋引擎,網路廣告,SEO,廣告,關鍵字,關鍵字排名,網路行銷,網頁設計,網站設計,網站排名,搜尋引擎,網路廣告,EMBA,MBA,PMP,在職進修,專案管理,出國留學,EMBA,MBA,PMP,在職進修,專案管理,出國留學,EMBA,MBA,PMP,在職進修,專案管理,出國留學,婚宴,婚宴,婚宴,婚宴,漢高資訊,漢高資訊,比利時,比利時聯合商學院,宜蘭民宿,台東民宿,澎湖民宿,墾丁民宿,花蓮民宿,SEO,找工作,汽車旅館,阿里山,日月潭,阿里山民宿,東森購物,momo購物台,pc home購物,購物漢高資訊,漢高資訊,在職進修,漢高資訊,在職進修,住宿,住宿,整形,造型,室內設計,室內設計,漢高資訊,在職進修,漢高資訊,在職進修,住宿,美容,室內設計,在職進修,羅志祥,周杰倫,五月天,住宿,住宿,整形,整形,室內設計,室內設計,比利時聯合商學院,在職進修,比利時聯合商學院,在職進修,漢高資訊,找工作,找工作,找工作,找工作,找工作,蔡依林,林志玲

Anonymous said...

酒店喝酒,禮服店,酒店小姐,傳播妹,便服店,鋼琴酒吧,酒店兼職,酒店兼差,酒店打工,伴唱小姐,暑假打工,酒店上班,酒店兼職,ktv酒店,酒店,酒店公關,酒店兼差,酒店上班,酒店打工,禮服酒店,禮服店,酒店小姐,酒店兼差,暑假打工,酒店小姐,台北酒店,禮服店 ,酒店小姐,酒店經紀,酒店兼差,寒假打工,酒店小姐,台北酒店,禮服店 ,酒店小姐,酒店經紀,酒店兼差,暑假打工,酒店小姐,台北酒店,禮服店 ,酒店小姐,酒店經紀,酒店兼差,寒假打工,台北酒店,禮服店 ,酒店小姐,酒店經紀,酒店兼差,暑假打工,酒店小姐,台北酒店,禮服店 ,酒店小姐,酒店兼差,暑假打工,酒店小姐,台北酒店,禮服店 ,酒店小姐,酒店經紀,酒店兼差,寒假打工,酒店小姐,台北酒店,禮服店 ,酒店小姐,酒店經紀,酒店兼差,暑假打工,酒店小姐,台北酒店,禮服店 ,酒店小姐,酒店經紀,酒店兼差,寒假打工,酒店小姐,台北酒店,禮服店 ,酒店小姐,酒店經紀,酒店兼差,暑假打工,酒店小姐,禮服店 ,酒店小姐,酒店經紀,酒店兼差,寒假打工,酒店小姐,禮服店 ,酒店小姐,酒店經紀,酒店兼差,暑假打工,酒店小姐,禮服店 ,酒店小姐,酒店經紀,酒店兼差,寒假打工,酒店小姐,禮服店 ,酒店小姐,酒店經紀,酒店兼差,暑假打工,酒店小姐,酒店傳播,酒店經紀人,酒店,酒店,酒店,酒店 ,禮服店 , 酒店小姐,酒店經紀,酒店兼差,暑假打工,招待所,酒店小姐,酒店兼差,寒假打工,酒店上班,暑假打工,酒店公關,酒店兼職,禮服店 , 酒店小姐 ,酒店經紀 ,酒店兼差,暑假打工,酒店,酒店,酒店經紀,

百科 said...

佰鳴, 核潛艇, 標準體重, 減肥, 紅酒, 室內設計, 美甲, 美白, 風水, 瑜伽, 月子, 禮儀‎, Speed Dating, 約會, 化妝, 八字, 旅遊, 紋身, 遊艇‎, 泰拳, 瑜伽‎, 自由行‎, 針灸, 失眠, 脫髮‎, 催眠, 抑鬱症, 焦慮症, 豐胸, 美容, 外匯, 黃金

Anonymous said...

福~
「朵
語‧,最一件事,就。好,你西...............................................................................................................................-...相互
,以讓>它使...................

Anonymous said...

[url=http://tw.myblog.yahoo.com/jw!BIBoU5SeBRs21nb_ajFpncbTqXds]酒店經紀[/url],[url=http://tw.myblog.yahoo.com/jw!qZ9n..6QEhhc0LkItOBm]酒店小姐[/url],[url=http://blog.xuite.net/jkl338801/blog]酒店兼職[/url]酒店經紀PRETTY GIRL,[url=http://mypaper.pchome.com.tw/news/thomsan/3/1310065116/20080905040949]酒店上班[/url],酒店工作 PRETTY GIRL[url=http://www.taipeilady.com]酒店[/url]酒店打工PRETTY GIRL,[url=http://tw.myblog.yahoo.com/jw!qZ9n..6QEhhc0LkItOBm]禮服店[/url],[url=http://mypaper.pchome.com.tw/news/jkl338801]酒店工作[/url]酒店經紀 彩色爆米花,[url=http://tw.myblog.yahoo.com/jw!rybqykeeER6TH3AKz1HQ5grm]酒店喝酒[/url],酒店兼差PRETTY GIRL