0

I'm trying to add the results of a loop to an array.

Here is my code:

  <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")>

The goal is to get an array of all mobile numbers.

My code isn't working and I'm after some research, I'm not sure what to do. Any ideas?

If possible I'd like to use non-cfscript for my solution but if using cfscript is easier, that's fine.

2 Answers 2

4

As Adam points out, the ArrayAppend needs to be inside the loop. You'll also need to remove the quotes around the "recipient" in your call to ArrayAppend, otherwire you'll have an array of the String "recipient".

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. On a related note, my code errors because of the mobileNo = mobileNo.replaceAll It does not like the replaceAll and I'm not sure why. Maybe I should start another question. When I comment out mobileNo = mobileNo.replaceAll, it does not error with the other replaceAll.
replaceAll is a java method of the String class, so it's not directly supported in ColdFusion but can be used. Look here for an example of Ben Nadel using it though: bennadel.com/blog/…
@barnyr Thanks. Adding JavaCast didn't seem to help but will try and solve it.
@Alias - Unless there is a specific reason for using String.replaceAll, just use CF's reReplace function help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/…
1

Your arrayAppend() needs to be inside the loop, otherwise you're just appending the last result after the loop has finished.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.