Enjoy an ad free experience by logging in. Not a member yet? Register.

Results 1 to 6 of 6
Thread: problem with nested tables

02092007, 02:40 PM #1
 Join Date
 Jan 2007
 Posts
 9
 Thanks
 0
 Thanked 0 Times in 0 Posts
problem with nested tables
Hi ,
I would like to dynamically populate tables
var bloc = new Array();which contain nested arrays(with constant size)
bloc[0]= new array(10);
bloc[1]= new array(10);
// till bloc[k]
The T table containing my values are constitued from inner array with different lengths
var T= new Array();
T[0], T[1], T[2],till T[n]
I start to fill the bloc[0] with T[0] and if the length of T[0] is smaller than 10 I go on with T[1]
But I have the constraint to have unique element per bloc
I have already checked than the individual table T[0], T[1], T[n]...have unique elements
for exemple
T[0]= ['1','2','3','4','5','6','20'];
T[1]= ['30','20','10','11'];
then bloc[0]should be['1','2','3','4','5','6','20','30','10','11']
Code:function Test(){ var k = 0; //counter of block var j=0;// loop in block var n=0; //counter of nested T var i=0;// loop in T[] var found; bloc[0][0]=T[0][0]; //initialisation: the first element in the first bloc come from the first elemt of T[0] for (i=0; i<(T[n].length);i++){ //i is the counter of element in T[] if(i%10==0)k++; //if already 10 elements in the block then a new bloc created if (bloc[k][j] == T[n][i]) { si element found do nothing found = true; //break; } if (!found){ bloc[k][bloc[k].length] = T[n][i]; // then put it in the block } j++; } n++; alert(bloc); }
Problem with loop?
many thanks for your ideas
02092007, 04:32 PM
#2
you have problems not with nested tables...u must use nested loops...try again with 2 for loops
1. for the T[0], T[1] ... T[T.length]
> 2. for the elements insided every T[n]
02092007, 04:57 PM
#3
 Join Date
 Jan 2007
 Posts
 9
 Thanks
 0
 Thanked 0 Times in 0 Posts
Hi shyam
thank for the advice
but I don't know how to do it.
Can you ,please,be more explicit
I have though to do something like for
for (var i=0;i<(T[0].length);i++){
bloc[0][i] = T[i];
}
but I don't know how many T[n] I have
and how can I go on with the next T[]
Thank for your interest
02092007, 05:21 PM
#4
Code:for ( i = 0; i < T.length; i++ ) { for ( j = 0; j < T[i].length; j++ ) { // T[i][j] } // inner j } // outer i
02092007, 05:34 PM
#5
 Join Date
 Jan 2007
 Posts
 9
 Thanks
 0
 Thanked 0 Times in 0 Posts
thank Shyam for your explanation
The function looks better but I still have problem to go to the next bloc after 10 elementsCode:function Test() { var k = 0; for (var i=0; i<T.length; ++i) { for (var j=0; j<T[i].length; ++j) { bloc[k].push(T[i][j]); if (bloc[k].length == 10) { ++k; bloc[k] = []; } } alert(bloc[k]); } }
02102007, 09:18 AM
#6
 Join Date
 Jan 2007
 Posts
 9
 Thanks
 0
 Thanked 0 Times in 0 Posts
Now it is working
I have forgotten some '('
Thank for you helps