• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Are global arrays interconnected ?

Community Expert ,
Jun 14, 2016 Jun 14, 2016

Copy link to clipboard

Copied

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);          // 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);               // 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 == 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?

TOPICS
Scripting

Views

376

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Enthusiast , Jun 14, 2016 Jun 14, 2016

Hi Klaus,

to copy the content of an array (and ONLY the content) you should use sth. like this:

Array1 = Array2.slice();

With Array1 = Array 2 you only create a second word for one and the same thing.

Votes

Translate

Translate
Enthusiast ,
Jun 14, 2016 Jun 14, 2016

Copy link to clipboard

Copied

Hi Klaus,

to copy the content of an array (and ONLY the content) you should use sth. like this:

Array1 = Array2.slice();

With Array1 = Array 2 you only create a second word for one and the same thing.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 14, 2016 Jun 14, 2016

Copy link to clipboard

Copied

LATEST

Whow!

Is this obvious - or a speciality of JavaScript?

I grew up with Fortran, Assembler, Pascal, Visual Basic and the script language AutoHotKey. Used JS not extensively on webpages...

Anyway: Klaus, thank You very much for this insight!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines