2012-10-26 86 views
0

我试图将循环的结果添加到数组。将循环添加到数组

这里是我的代码:

<cfset mobNumbers = ArrayNew(1)> 

<cfloop query = "staffLoop"> 
    <cfscript> 

     mobileNo = staffLoop.mobileno; 
     mobileNo = mobileNo.replaceAll("[^0-9]", ""); 
     ext = staffLoop.extension; 
     ext = ext.replaceAll("[^0-9]", ""); 

     if (Left(mobileNo, 2) == "07" || Left(mobileNo, 3) == "447") { 
     recipient = mobileNo; 
     } else if (Left(ext, 2) == "07" || Left(ext, 3) == "447") { 
     recipient = ext; 
     } else { 
     recipient = ""; 
     } 

     if (Left(recipient, 2) == "07") { 
     recipient = "447" & Right(recipient, Len(recipient) - 2); 
     } 

     if (Len(recipient) == 12) { 

     [send text code] 

     } 
    </cfscript> 
    </cfloop> 
<cfset ArrayAppend(mobNumbers, "recipient")> 

我们的目标是让所有的手机号码的数组。

我的代码不工作,我经过一番研究,我不知道该怎么做。有任何想法吗?

如果可能我想为我的解决方案使用非cfscript,但如果使用cfscript更容易,那很好。

回答

4

正如Adam指出的那样,ArrayAppend需要在循环内。您还需要在对ArrayAppend的调用中移除“收件人”周围的引号,否则您将有一个String“收件人”数组。

+0

哦,是的,好点。 –

+0

谢谢。在相关的说明,我的代码错误,因为'mobileNo = mobileNo.replaceAll'它不喜欢replaceAll,我不知道为什么。也许我应该开始另一个问题。当我注释掉'mobileNo = mobileNo.replaceAll'时,它不会与其他replaceAll错误。 – Alias

+2

replaceAll是String类的java方法,所以它不直接受ColdFusion支持,但可以使用。看看这里的Ben Nadel使用它的例子:http://www.bennadel.com/blog/1488-ColdFusion-Regular-Expressions-Do-Not-Support-Character-Class-Intersection-Or-Subtraction.htm – barnyr

1

你的arrayAppend()需要里面的循环,否则你只是追加循环完成后的最后结果。

相关问题