sandigcustomprinters.com Posted March 18, 2013 Share Posted March 18, 2013 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; Quote Link to comment Share on other sites More sharing options...
Dan Korn Posted March 18, 2013 Share Posted March 18, 2013 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. Quote Link to comment Share on other sites More sharing options...
sandigcustomprinters.com Posted March 19, 2013 Author Share Posted March 19, 2013 The rule does not realize that if a field is "", then the other numbers need to "move up" in the array position. See the attached example. Is this possible? Quote Link to comment Share on other sites More sharing options...
Dan Korn Posted March 19, 2013 Share Posted March 19, 2013 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? Quote Link to comment Share on other sites More sharing options...
sandigcustomprinters.com Posted March 19, 2013 Author Share Posted March 19, 2013 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. Quote Link to comment Share on other sites More sharing options...
sandigcustomprinters.com Posted March 21, 2013 Author Share Posted March 21, 2013 It seems that the array does not work in the manner I was trying to do. So, I eliminated it and wrote in the if statements to adjust the <br> position when variables have no value. attached. This will work for what I need. Thank you for the lesson on arrays. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.