Forum Discussion

tinauser's avatar
tinauser
Frequent Contributor
13 years ago

array as temporary variable

Hi,

can I save a jscript array as temporary variable?say I name a project variable of type object pr_arr.



Then in a script i do:



var arr = new Array();

arr["k1"] = "i1";

//..arr["k_n"] = "i_n";



is it correct the following assignment?

Project.Variables.pr_arr = arr;



I have problems then to use the variable as an array: indeed I can do:



for (el in arr)

{}

 but I can not do

for(el inProject.Variables.pr_arr )

{}



where is my error?

Thx

7 Replies


  • Hi Lorenzo,





    I have fixed the links in my previous post.





    I have created a sample script demonstrating how you can work with a Table type variable.

    function test()

    {

      var arr = new Array();

      arr["USA"] = "Washington";

      arr["UK"] = "London";

      arr["India"] = "New Delhi";

      

      for (var i in arr) {

        Log.Message(arr);

      }

      

      var tbl = copyArrayToTable(arr, "tblVar");

             

      for (var i = 0; i < tbl.rowCount; i++) {

        Log.Message(tbl.Item("Key", i) + ": " + tbl.Item("Value", i));

      }

      

      Log.Message(getItem("tblVar", "UK"));

    }









    function copyArrayToTable(arr, tblVarName)

    {

      if (false == Project.Variables.VariableExists(tblVarName)) {

        Project.Variables.AddVariable(tblVarName, "Table");

      }

      

      var tbl = Project.Variables.VariableByName(tblVarName);





      tbl.RowCount = 0;

      while (tbl.ColumnCount > 0) {

        tbl.RemoveColumn(0);

        tbl.Next();

      }

      

      tbl.AddColumn("Key");

      tbl.AddColumn("Value");

      

      for (var i in arr) {

        addItem(tblVarName, i, arr);

      }

      

      return tbl;

    }





    function addItem(varName, key, value)

    {

      var tbl = Project.Variables.VariableByName(varName);

      tbl.RowCount = tbl.RowCount + 1;

      tbl.Item("Key", tbl.RowCount - 1) = key;

      tbl.Item("Value", tbl.RowCount - 1) = value;

    }





    function getItem(varName, key)

    {

      var tbl = Project.Variables.VariableByName(varName);

      var iterator = tbl.Iterator;





      while (false == iterator.IsEOF()) {

        if (iterator.Value("Key") == key) {

          return iterator.Value("Value");

        }

        iterator.Next();

      } 

      

      Log.Error("The '" + key + "' item is not found");

      return null; 

    }
  • tinauser's avatar
    tinauser
    Frequent Contributor
    I´m sorry to insist, but this is a little frustrating...

    Is there anyway to store jscript array object, or activeXobject("Scripting.Dictionary") in variables, so that several function can have access to them?I could not figure a solution

  • AlexKaras's avatar
    AlexKaras
    Champion Level 3
    Hi Lorenzo,



    I am far not sure about JScript, but I have some recollection that in VBScript it was possible to store dictionaries in the project temporary variable, but in order to use it later, the value of the temporary variable had to be assigned to local script variable. I.e. something like this:

    Sub ConsumingProc

      Dim oDic



      Set oDic = Project.Variables.aPrjVarDic

      ' use oDic now

      ...



      ' save oDic in project variable if needed

      Set Project.Variables.aPrjVarDic = oDic

    End Sub



    You may try this approach with JScript.
  • Hi Lorenzo,



    Project variables of the Object type can store references to an application's objects, but not to scripting languages' objects.



    Please use variables of the Table type that can store table data.
  • tinauser's avatar
    tinauser
    Frequent Contributor
    alex

    if in java I do something like this

    function ConsumingProc()

    {

      var oDic = new ActiveXObject("Scripting.Dictionary");  

      oDic = Project.Variables.aPrjVarDic;

      // use oDic now

      oDic.Add("k1","i1"); 

     }

     I would already get an exceptio, as the statemen

        oDic = Project.Variables.aPrjVarDic;

    would change oDic from a dictionary object to an undefined object



    If I add the statement

        Project.Variables.aPrjVarDic = new ActiveXObject("Scripting.Dictionary");  



    then the following statement won´t give error



        oDic.Add("k1","i1"); 

        for (var el in oDic)

            log.Message(el);

    But the script will never go into the for loop and no message would be printed (actually, this happens indipendently from the assignment to oDict of the project object variable...).



    David

    using a Table variable, I think I´d have the following problem:

    let say I have this 

    Project.Variables.mTable of the type table

    and the table has two columns (say tabKeys and tabItems) , can I make a call like    

    Project.Variables.mTable[key1] to fetch the corrisponding item1 ?



    PS

    I cannot open your links 

     




  • tinauser's avatar
    tinauser
    Frequent Contributor
    Dear David,

    Thanks a lot for the reply.

    actually I ended up with the same subroutines as yours...wouldn´t it make sense to add such a common data structure as a dictionary natively in TC? Or are there few peopole with such  aneed and this workaround is judged enough for satisfied them?

    thanks a lot


  • Hi Lorenzo,





    I have registered a suggestion about the dictionary type variable in our database. Thank you.