This forum is closed to new posts and responses. Individual names altered for privacy purposes. The information contained in this website is provided for informational purposes only and should not be construed as a forum for customer support requests. Any customer support requests should be directed to the official HCL customer support channels below:

HCL Software Customer Support Portal for U.S. Federal Government clients
HCL Software Customer Support Portal



Apr 22, 2011, 2:13 PM
29 Posts

array error

  • Category: Server Side JavaScript
  • Platform: All
  • Release: 8.5.2
  • Role: Developer
  • Tags:
  • Replies: 4
 Hi, everybody!
 
I have the following SSJS problem: i create an array and populate it with data from documents, but I get error after I try to assign value to 3rd element of array. 
 
The code goes as follows: 
 var a=new Array(new Array(4),new Array(4));
 for(var i=0; i<dc.getCount();i++) {
print("i"+i);
a[i][0]="myItemValue1";
a[i][1]="myItemValue2";
doc=dc.getNextDocument();
};
 
Debug shows that I rotate till i=2 and then I get an error saying a[i] is not defined and cannot be accessed as Array. 
 
Help me, please. 
 
Thank you! 
Apr 23, 2011, 10:37 AM
261 Posts
Re: array error
Hi Ruslan,
 
I've found multi-dimensional arrays always hard to work with. Why don't you try creating an array of objects, e.g.:

var a = [];  //creates the array

for (var i=0; i<dc.getCount(); i++) {
 a.push( { "value1" : "myItemValue1", "value2" : "myItemValue2" } );  //adds an object to the array with 2 properties: value1 and value2
 doc = dc.getNextDocument();
}

Apr 23, 2011, 11:34 AM
2 Posts
Re: array error
Hi, Mark!
 
Yeah, I used push method in another place in my script and it worked well, just in this very place it looked like to be easier to work via multidimentional array, but it really seems like a trick to work on.
Apr 25, 2011, 8:00 PM
64 Posts
It looks to me like it's working correctly...
- The statement
 
var a = new Array(new Array(4), new Array(4));

is creating a Java array with two elements, one a Java array with 4 elements, and the other a Java Array with 4 elements.  Those two should have indexes 0 and 1, so when you reach 2, it fails, exactly like it's supposed to.  Since it's Java, and not JS, it won't automagically tack on new elements for you.
 
- I would look up how Java does multi-dimensional arrays and use that.  ArrayList of ArrayList comes to mind, for a fully dynamic jagged array that would work for smaller amounts of data.

Hope this helps...
Apr 27, 2011, 11:18 AM
29 Posts
Re: It looks to me like it's working correctly...
 Thank you,  David, I'll work on that.

This forum is closed to new posts and responses. Individual names altered for privacy purposes. The information contained in this website is provided for informational purposes only and should not be construed as a forum for customer support requests. Any customer support requests should be directed to the official HCL customer support channels below:

HCL Software Customer Support Portal for U.S. Federal Government clients
HCL Software Customer Support Portal