Search code examples
arraysloopscoldfusioncoldfusion-8

Adding loop to array


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.


Solution

  • 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".