Page 1 of 1

rename variable in a loop

PostPosted: Thu Dec 11, 2014 7:34 pm
by irene.meyer
Hello,

I have a loop that returns a number of array's
Code: Select all
for(var r = 1; r<=rMax-1; r++)
{
var   P =datasetarray.getRowAsArray(r);
}


[0.998,0.001,0.0005,0.0005,0]
[0,0.995,0.003,0.001,0.001]
[0,0,0.995,0.0025,0.0025]
[0,0,0,0,1]

I need to construct a number of variables each representing one of the arrays

var S1 = [0.998,0.001,0.0005,0.0005,0]
var S2 = [0,0.995,0.003,0.001,0.001]
var S3 = [0,0,0.995,0.0025,0.0025]
var S4 = [0,0,0,0,1]

is there a way to name the variables like:

var S[r] = P[r]?

I would appreciate any help.

Irene

Re: rename variable in a loop

PostPosted: Thu Dec 11, 2014 8:31 pm
by ROCLASI
Hi Irene,

Like this?

Code: Select all
var S = new Array();
for (var r = 1; r<=rMax-1; r++) {
    // push the RowAsArray result into the S array
    S.push(datasetarray.getRowAsArray(r));
}


Hope this helps.

Re: rename variable in a loop

PostPosted: Fri Dec 12, 2014 11:26 am
by irene.meyer
Hi Robert,

thanks for the reply. In my previous post i messed up the variables, sorry for that.
I try to explain a little bit better what I'm trying to do. I wrote a static code for test and it works.

Code: Select all
var S = new Array(1,0,0,0,0);   
   var state = forms.z_all_objects.obj_state_uncertain
   var SR = new Array(1,0,0,0,0);
   var R = new Array(1,0,0,0,0);
   
   //transition matrix
   var P1= new Array(0.998,0.001,0.0005,0.0005,0.00);
   var P2= new Array(0.00,0.995,0.003,0.001,0.001);
   var P3= new Array(0.00,0.00,0.995,0.0025,0.0025);
   var P4= new Array(0.00,0.00,0.00,0.99,0.01);
   var P5= new Array(0.00,0.00,0.00,0.00,1.00);
   var j = 0
   var noyears =5
   for (var i=1;i<=noyears;i++)
   {
   
   
       R[0] = S[0]*P1[0]+S[1]*P2[0]+S[2]*P3[0]+S[3]*P4[0]+S[4]*P5[0];
       R[1] = S[0]*P1[1]+S[1]*P2[1]+S[2]*P3[1]+S[3]*P4[1]+S[4]*P5[1];
       R[2] = S[0]*P1[2]+S[1]*P2[2]+S[2]*P3[2]+S[3]*P4[2]+S[4]*P5[2];
       R[3] = S[0]*P1[3]+S[1]*P2[3]+S[2]*P3[3]+S[3]*P4[3]+S[4]*P5[3];
       R[4] = S[0]*P1[4]+S[1]*P2[4]+S[2]*P3[4]+S[3]*P4[4]+S[4]*P5[4];
   
      
       S=R........




In the real code S is a text field from table Objects containing eg.: 0.4044216661597251,0.0659004623761775,0.1807015511863892,0.34897632027770825;
P is a dataset from table Probabilities with a number of records, in this case :

[0.998, 0.001, 5.0E-4, 5.0E-4, 0.0]
[0.0, 0.995, 0.003, 0.001, 0.001]
[0.0, 0.0, 0.995, 0.0025, 0.0025]
[0.0, 0.0, 0.0, 0.0, 1.0

What I need to do is multiply the S with the P array, see the R array;

In the real code I wrote I get the array P[0], P[1] etc. but what I need is P1 = P[0], P2=P[1] etc.

Code: Select all
for(var r = 1; r<=rMax-1; r++)
      {
      var   P =datasetarrayP.getRowAsArray(r);
          P=P.filter(function(item){return item != null});
      }


I hoop I made myself a little bit clearer now.

Thanks
Irene

Re: rename variable in a loop

PostPosted: Mon Dec 15, 2014 4:28 pm
by Rene van Veen
Hi Irene,

I don't think its a good idea... but its working:
Here is a quick example:

Code: Select all
var vDataset = databaseManager.createEmptyDataSet()
vDataset.addRow(['data1', 'data2'])
vDataset.addRow(['dataaa1', 'dataaa2'])

for(var i = 1; i <= vDataset.getMaxRowIndex(); i++)
{
   var vRow = vDataset.getRowAsArray(i)
   eval("var row" + i + " = []")
   eval("var row" + i + " = vRow")
}

for(var j = 1; j <= 2; j++)
{
   application.output(eval("row" + j))
}


I hope this is getting the correct result for you :)

Re: rename variable in a loop

PostPosted: Tue Dec 16, 2014 6:26 am
by ROCLASI
Hi Irene,

irene.meyer wrote:In the real code I wrote I get the array P[0], P[1] etc. but what I need is P1 = P[0], P2=P[1] etc.


Why the need for separate variables for this? Why can't you use the array for this?
Anyway, another approach would be to use named properties in an object to get your P1, P2 etc.
Like so:
Code: Select all
var Result = new Object();
for ( var i = 0 ; i < P.length ; i++) {
    Result['P'+i] = P[i];
}

application.output(Result.P0);
application.output(Result.P1);
application.output(Result.P2);
application.output(Result.P3);
application.output(Result.P4);


Hope this helps.

Re: rename variable in a loop

PostPosted: Tue Dec 16, 2014 12:20 pm
by irene.meyer
Hi Robert and Rene,

eventually I figure it out this weekend. The loop was the answer.

Code: Select all

var serverName = databaseManager.getDataSourceServerName(forms.game_objects1.controller.getDataSource());
var queryS = "SELECT obj_s_array FROM objects Where obj_id =  " + id;
var datasetarrayS = databaseManager.getDataSetByQuery (serverName, queryS, null, 1);
var queryP = "SELECT prob_1,prob_2,prob_3,prob_4,prob_5,prob_6,prob_7,prob_8,prob_9,prob_10 FROM probabilities";
var datasetarrayP = databaseManager.getDataSetByQuery (serverName, queryP, null, 10);
var P = databaseManager.createEmptyDataSet()

for(var i = 0; i<+datasetarrayP.getMaxRowIndex();i++)
   {
   var filter = datasetarrayP[i].filter(function(item){return item != null});
   P.addRow(filter)
   }   
   var R= new Array();
   var S = datasetarrayS.getValue(1,1).split(',');
   var len = S.length-1;
   for(var r=0; r<=len;r++)
   {
      var  sum1 = 0
      for(var p = 0; p<=len; p++)
      {
            var sum = 0 ;
            var index = r

               sum += S[p] * P[p][index];
               sum1 = sum1 + sum
      }
      R[r] = +sum1

   }


Thank you both for helping.

Cheers
Irene