These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Simple Array Help

GlennOwns
Registered: Jun 30 2010
Posts: 18
Answered

Can someone simplify my code? I just don't know how to implement arrays! I tried to learn but it just confused me XD.
I have 11 fields. Cost 10[01-12] How could i set up an array for these?
 
event.value = +getField("Cost 1001").value + +getField("Cost 1002").value + +getField("Cost 1003").value + +getField("Cost 1004").value + +getField("Cost 1005").value + +getField("Cost 1006").value + +getField("Cost 1007").value + +getField("Cost 1008").value + +getField("Cost 1009").value + +getField("Cost 1010").value + +getField("Cost 1011").value + +getField("Cost 1012").value;
if ((event.value == 0) && (getField("Cost 1999").valueAsString === "")) event.value = "";

My Product Information:
Acrobat Pro 10.0.1, Windows
try67
Expert
Registered: Oct 30 2008
Posts: 2398
Read this tutorial: http://w3schools.com/js/js_obj_array.asp

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

GlennOwns
Registered: Jun 30 2010
Posts: 18
Thanks for the quick reply try67,

However, I've already read through w3 before. Love the site, very useful. And I know what arrays are, but I just can't figure out how to do the run through of 1-10. or 1-12, or whatever it is.

I can do the simple name arrays, but how do you run through a range? Like counting in excel would. Thanks for your help.. =/
try67
Expert
Registered: Oct 30 2008
Posts: 2398
For that you can use a for-loop, like so:

var myArray = new Array("Saab","Volvo","BMW");
for (i=0; i < myArray.length; i++) {
console.println(myArray[i]);
}

Or you can use the for-in-loop, like so:

var myArray = new Array("Saab","Volvo","BMW");
for (i in myArray) {
console.println(myArray[i]);
}

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

GlennOwns
Registered: Jun 30 2010
Posts: 18
Now I have this, but I don't understand what's wrong with it?

var myArray = new Array("01","02","03","04","05","06","07","08","09","10","11","12");
for (i=0; i < myArray.length; i++) {
console.println(myArray[i]);

event.value = getField("myArray").value;
if ((event.value == 0) && (getField("Cost 1999").valueAsString === "")) event.value = "";In other words, I'm lost. console.printIn isn't what I'm lookin for is it..? I'll keep digging..
GlennOwns
Registered: Jun 30 2010
Posts: 18
Accepted Answer
Hey try67, thanks for your help. I just stuck with the non-array. I know it's sloppy but I won't know the difference at work.

Thanks!
try67
Expert
Registered: Oct 30 2008
Posts: 2398
My example was supposed to demonstrate how to access an item in the array, using the array's name followed by the index in square brackets. console.println just prints the names of the items in the array to the console.

As it is now, you're trying to access a field called "myArray" in this line:
event.value = getField("myArray").value;

What you want to do is the following:

// define the array
var myArray = new Array("01","02","03","04","05","06","07","08","09","10","11","12");

// initialise the value
event.value = 0;

// loop over the array and add all of the values of the fields defined in it
for (i=0; i < myArray.length; i++) {
event.value += this.getField(myArray[i]).value;
}

// if the total value is 0 and field "Cost 1999" is empty, reset the final value
if ((event.value == 0) && (this.getField("Cost 1999").valueAsString === "")) event.value = ""

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com