Large array initialization

Former Member
Former Member
I am having trouble initializing a large array with every value set to 1. The following code causes following exception: Watchdog Tripped Error - Code Executed Too Long.
var array = new [10000];
for(var i = 0; i < 10000; i++)
{
array= 1;
}
[/CODE]
What can I do instead of iterating through the array and setting each index to 1?
  • You could try loading it as a Jsondata resource which is of array type - might work for you.
  • The watchdog is doing what's it's designed to do - to stop things if your code run too long in a single call from the vm.

    The first question, is why do you have to set them all to 1? They'll all be null when the array is created, so you could use that.

    Is it something where you'll be using all 10000 on a regular basis? (if this triggers the watchdog, walking all 10k and doing some math will trigger the watchdog too)

    When I use a large array, it's to track things as they happen - like saving a location every few seconds, and in that case, I use an index to the next free entry, and then for display, I have a fixed number of points to use - say 200. Then based on the amount of the array with data, use every one, every other one, every 3rd one, etc

    if you really want to set all 10k to 1, you could use a timer, and do something like set 500 each time the timer ticks, as that way you'll be leaving your code before the watchdog trips.