2 Replies Latest reply on Jun 14, 2016 12:17 PM by K.Daube

    Are  global arrays interconnected ?

    K.Daube Level 1

      Dear friends
      I am more than puzzled about the following

       

      This is the global definition of two arrays:

      var gasLocations      = [];                       // filled from template or current document  
      var gasLocationsPredef= [];                       // filled in SetUpArraysEtc () 
      

      In function SetUpArraysEtc () they are filled:

      // locations for the list in the dialogue
      gasLocations1   = ['Above()', 'Below()', 'Left()', 'Right()']; // used in UnknownElement concatenated to gasFunctions
      var asLocations2    = ['[COL m]', '[ROW n]', '[CELL n, m]']; 
      gasLocationsPredef = gasLocations1.concat(asLocations2); // These must not be deleted from the list
      gasLocations    = gasLocationsPredef;
      

       

      Later in a dialog function array gasLocations shall be updated. I have set a breakpoint just before at the crucial point:

       

      function ButtonAddLocation () { // ================================================================
      // Attention: this regex is different from that in ChecktermElements!
        var re_Def = /(\[ROW +\d+\]|\[COL +\d+\]|\[CELL +\d+, +\d+\]|Left *\(\d*\)|Right *\(\d*\)|Above *\(\d*\)|Below *\(\d*\))/;
        var item = wPalDS.p0.tabL.g1.p2.g1.sLocation.text;
        if (item.search(re_Def)) {
          alert (gsDSSyntaxErr);
          return;
        } 
      $.bp(true);
        UpdateListLocations (wPalDS.p0.tabL.g1.p1.listLocations, item);
        FillItemList (wPalDS.p0.tabL.g1.p1.listLocations,  gasLocations);
        PutRefPageItems ("Ref-Locations", "");          // update reference page
      } // --- end ButtonAddLocation
      
      function ButtonDelLocation () { // ================================================================
        var lstIndex = wPalDS.p0.tabL.g1.p1.listLocations.selection.valueOf();
        var locName = gasLocations[lstIndex];
        if (IsInArray (gasLocationsPredef, locName)) {
          alert ("Predfeined items can not be deleted");
          return;
        };
        DeleteItemLocations (wPalDS.p0.tabL.g1.p1.listLocations, locName)
        PutRefPageItems ("Ref-Locations", "");          // update reference page
      } // --- end ButtonDelLocation
      
      function UpdateListLocations (oList, item) { // ====================================================
      // update global array and list in dialogue;
        var locArray = gasLocations, j, nItems;
        locArray.push (item);
        gasLocations = locArray.sort ();                // update global array
        nItems = gasLocations.length;
        oList.removeAll ();                             // clear the list
        for (j = 0; j < nItems; j++) {
          oList.add ("item", gasLocations[j]);          // add item
        }
      } // --- end UpdateListLocations
      
      function DeleteItemLocations (oList, item) { // ====================================================
      // List is rebuilt from array. Function array.splice not available in ES
        var index = IsInArray (gasLocations, item);
        var lower = gasLocations.slice (0, index);      // lower part
        var upper = gasLocations.slice (index+1);       // upper part
        gasLocations    = lower.concat(upper);
        FillItemList (oList, gArray);
      } // --- end DeleteItemLocations
      
      function FillItemList (oList, aSource) { // =====================================================
        var j, nItems = aSource.length;
        oList.removeAll ();                             // clear the list
        for (j = 0; j < nItems; j++) {
          oList.add ("item", aSource[j]);               // add item
        }
      } // --- end FillItemList
      function IsInArray (array, what) { //==============================================================
      // indexOf is not supported in ExtendScript...
        var jl;
        for (j = 0; j < array.length; j++) {
          if (array [j] == what ) { return j;}
        }
        return null;     // not found in array
      } // --- end IsInArray
      

      Now guess what? After UpdateListLocations both arrays have the same content! But gasLocationsPredef should not be updated, as it serves as a reference: Items in the selection list corresponding to items this array must not be deleted from the list. The function ButtonDelLocation always finds the newly inserted item and does not accept the deletion.
      Once I read a warning about global variables - but I carefully avoid to use them as output-arguments in functions (only objects are handled 'by reference').

       

      Who has eagle eyes to spot my error?