Jump to content

assigning variables via an array


Recommended Posts

I am trying to assign 4 possible phone numbers into variables so I can control the stacking order selected by the user. The user does not have to use all four numbers (direct, toll free, cell and fax). Only 3 numbers can fit on one line. The numbers will always list in teh same order. For example:

 

direct | toll free | Cell

Fax

 

direct

cell | fax

 

tollfree | cell | fax

 

etc. etc.

 

I put in a start to an array, but it is not correct. Can you help me with the array? It needs to assign Phone1, Phone2, Phone3, Phone4 based on the phone fields populated by the user.

 

var phonestack = "";

var Phone1 = "";

var Phone2 = "";

var Phone3 = "";

var Phone4 = "";

 

 

//create an array to apply phones to variable Phone placeholders

var phoneArray = [Rule("RULE_directFormat"), Rule("RULE_tollfreeFormat"), Rule("RULE_cellFormat"), Rule("RULE_faxFormat")];

 

{

for (i = 0; i < phoneArray.length; i++){

Phone = phoneArray;

}

 

}

 

//now assign the variables to the stacking order

if (Field("Phone position") == "1|2|3 and 4")

{

phonestack = AppendText(phonestack, " ", Phone1);

phonestack = AppendText(phonestack, " | ", Phone2);

phonestack = AppendText(phonestack, " | ", Phone3);

phonestack = AppendText(phonestack, "<br>", Phone4);

}

 

if (Field("Phone position") == "1|2 and 3|4")

{

phonestack = AppendText(phonestack, " ", Phone1);

phonestack = AppendText(phonestack, " | ", Phone2);

phonestack = AppendText(phonestack, "<br>", Phone3);

phonestack = AppendText(phonestack, " | ", Phone4);

}

 

if (Field("Phone position") == "1 and 2|3|4")

{

phonestack = AppendText(phonestack, " ", Phone1);

phonestack = AppendText(phonestack, "<br>", Phone2);

phonestack = AppendText(phonestack, " | ", Phone3);

phonestack = AppendText(phonestack, " | ", Phone4);

}

 

return phonestack;

Link to comment
Share on other sites

If you want to use an array, then you need to declare an actual Array object, not just variables with successive numbers in their names, and you need to either populate the array in an Array literal, or call the Array.push method to add items to the array. Then you need to access the array items with square brackets to indicate the index; i.e. "Phone[1]".

 

Actually, you're already creating an array named phoneArray. So just change the name of that variable to Phone, and index the array elements, and things should work, like so:

var phonestack = "";

//create an array to apply phones to variable Phone placeholders
var Phone = [Rule("RULE_directFormat"), Rule("RULE_tollfreeFormat"), Rule("RULE_cellFormat"), Rule("RULE_faxFormat")];

//now assign the variables to the stacking order
if (Field("Phone position") == "1|2|3 and 4")
{
phonestack = AppendText(phonestack, " ", Phone[1]);
phonestack = AppendText(phonestack, " | ", Phone[2]);
phonestack = AppendText(phonestack, " | ", Phone[3]);
phonestack = AppendText(phonestack, "<br>", Phone[4]);
}

if (Field("Phone position") == "1|2 and 3|4")
{
phonestack = AppendText(phonestack, " ", Phone[1]);
phonestack = AppendText(phonestack, " | ", Phone[2]);
phonestack = AppendText(phonestack, "<br>", Phone[3]);
phonestack = AppendText(phonestack, " | ", Phone[4]);
}

if (Field("Phone position") == "1 and 2|3|4")
{
phonestack = AppendText(phonestack, " ", Phone[1]);
phonestack = AppendText(phonestack, "<br>", Phone[2]);
phonestack = AppendText(phonestack, " | ", Phone[3]);
phonestack = AppendText(phonestack, " | ", Phone[4]);
}

return phonestack;

Of course, once you have an array, and you're accessing successive elements, that logic can usually be reduced with a "for" loop. So the rule can be written like so:

//create an array to apply phones to variable Phone placeholders
var Phone = [Rule("RULE_directFormat"), Rule("RULE_tollfreeFormat"), Rule("RULE_cellFormat"), Rule("RULE_faxFormat")];

//now assign the variables to the stacking order
var delims = [];
switch (Field("Phone position"))
{
   case "1|2|3 and 4":
       delims = [" ", " | ", " | ", "<br>"];
       break;

   case  "1|2 and 3|4":
       delims = [" ", " | ", "<br>", " | "];
       break;

   case "1 and 2|3|4":
       delims = [" ", "<br>", " | ", " | "];
       break;

   default:
       throw "Unrecognized Phone position";
}

var phonestack = "";
for (var i = 1; i <= 4; i++)
   phonestack = AppendText(phonestack, delims[i], Phone[i]);

return phonestack;

It could be reduced even further by simply replacing the " and " in the "Phone Position" with "<br>" and the other delimiters with pipes.

Link to comment
Share on other sites

Well, your original question was just about how to use an array in general. How to suppress certain items is a separate issue.

 

I guess I'm confused about the purpose of the "Phone position" field then. If the field value is "1 and 2|3|4", and "1" is empty, then do 2, 3, and 4 still go on the same line? Or does it become "2<br>3 4" instead?

 

Also, can you post the AppendText function?

Link to comment
Share on other sites

If the field value is "1 and 2|3|4", and "1" is empty, then, yes, it becomes "2<br>3 | 4" instead. or

"Phone[0]<br>Phone[1] | Phone [2] | Phone[3]"

Phone[3] should not appear because it would have no value.

 

Not "<br>2|3|4"

 

If they have only 2 phone fields used, it would appear

"Phone[0]<br>Phone[1]" regardless of what fields are used.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...