17 Replies Latest reply on Jun 28, 2017 6:50 AM by Liphou

# Titling the entries of array of array

Hi all'

while studying the the array of array I DEFINE a as follow

a=[

[ A=[134, 500] , B=[134,457] , C=[134,420]],

[ A=[150, 400] , B=[186,400] , C=[220,400]],

[ A=[230, 420] , B=[230,436] , C=[230,457]],

];

why the statement

a[0].B;

give undefined

i expect to give me :

134,457

i get this idea from a response done by Peter Kahrel

which learned lots from him

• ###### 1. Re: Titling the entries of array of array

Hi

Your A, B and C values are working as follows

```A=[134, 500];
B=[134,457];
C=[134,420];
A=[150, 400];
B=[186,400];
C=[220,400];
A=[230, 420];
B=[230,436];
C=[230,457];
```

Therefore if you A ends up as [230, 420] which is the last value it was assigned.

a[0] has the value of [[134, 500], [134,457], [134,420]]

The structure that you are looking for is.

```a = [{
A: [134, 500],
B: [134, 457],
C: [134, 420]
}, {
A: [150, 400],
B: [186, 400],
C: [220, 400]
}, {
A: [230, 420],
B: [230, 436],
C: [230, 457]
}];
```

An alternative but usually not recommended could be

```a = [[], [], [] ];
a[0]['A'] = [134, 500];
a[0]['B'] = [134, 457];
a[0]['C'] = [134, 420];

a[1]['A'] = [150, 400];
a[1]['B'] = [186, 400];
a[1]['C'] = [220, 400];

a[2]['A'] = [230, 420];
a[2]['B'] = [230, 436];
a[2]['C'] = [230, 457];
```

Or slightly better

```a = [[], [], [] ];
a[0].A = [134, 500];
a[0].B = [134, 457];
a[0].C = [134, 420];

a[1].A = [150, 400];
a[1].B = [186, 400];
a[1].C = [220, 400];

a[2].A = [230, 420];
a[2].B = [230, 436];
a[2].C = [230, 457];
```

HTH

Trevor

• ###### 2. Re: Titling the entries of array of array

Hi Saeed,

think again.

What you are doing is to assign values to variables a, A, B and C.

You do the same statement three times with A, B and C.

But you are using different values overwriting the one you are doing before.

The last statement counts.

So a[0] will result in: [ [230, 420] , [230,436] , [230,457] ]

So a[1] in: [ [230, 420] , [230,436] , [230,457] ]

So a[2] in: [ [230, 420] , [230,436] , [230,457] ]

B is [230,436] ,

thus a[0][1] is: [230,436]

If you want to use array a like that:

a[0].B

a[1].B

a[2].B

where you expect different values for every statement you could use associative arrays or objects stored in array a.

Or more simply put work directly with objects like that:

```var a =
[
{ A:[134, 500] , B:[134,457] , C:[134,420] },
{ A:[150, 400], B:[186,400] , C : [220,400] },
{ A:[230, 420] , B:[230,436] , C:[230,457] }
];

a[0].B // results in: [134,457]
a[0]["B"] // results in: [134,457]
```

If you want longer variable names for A, B and C where white space or other characters are involved that are not allowed in variable names, you have to use a string and cannot use the dot notation:

```var a =
[
{ "My wonderful A":[134, 500] , "My fantastic B":[134,457] , "My extraordinary C":[134,420] },
{ "My wonderful A":[150, 400], "My fantastic B":[186,400] , "My extraordinary C" : [220,400] },
{ "My wonderful A":[230, 420] , "My fantastic B":[230,436] , "My extraordinary C":[230,457] }
];

a[0]["My fantastic B"] // results in: [134,457];
a[0]."My fantastic B" // Syntax error
```

Initially I prepared a longer reply, but now that Trevor answered I leave it at that.

Regards,
Uwe

• ###### 3. Re: Titling the entries of array of array

Hi Trevor,

why do you think, that an expression like that:

```var a = [ [], [], [] ];
a[0]['A'] = [134, 500];
```

or perhaps like that ( which is the same ):

```var a = [ {}, {}, {} ];
a[0]['A'] = [134, 500];
```

is not recommended?

Regards,
Uwe

• ###### 4. Re: Titling the entries of array of array

Hi Uwe

For performance it seems that using an associative array could in fact be way faster than using objects.

I did not do test myself.

I was referring to setting up a small array which I think is more convenient to setup in one deceleration than using multiple assignments.

Also in the jsx context one can use a.toSource()  with the object method but not with the associative array method.

I shall try look later into the performance issues myself later.

I could very well be wrong with my recommendation and I think you are correct to question it.

Regards

Trevor

• ###### 5. Re: Titling the entries of array of array

Hi Trevor and Laubender,

Thank you both for your quick responses.

both answers is exactly what I was looking for.

Thank you very much again, Trevor and Laubender.

Regards,

Saeed

• ###### 6. Re: Titling the entries of array of array

Peter's example is technically wrong How very rare. (OTOH it was only to highlight the particular order of pathpoint points and not framed as "actual code".)

• ###### 7. Re: Titling the entries of array of array

Peter's example  give a hint to answer the question "How to declare array of array" which has no solution if you in this forum.

Moreover when i tried to build Pascal's triangle it is straight  forward

```n=10
a=pascalNumbers(n)
for (i=0;i<a[n].length;i++)
function pascalNumbers(n){
var pascal=[];
var row0=[1];
var row1=[1,1];
pascal.push(row0);
pascal.push(row1);
for (i=2;i<n+1;i++){
newRow=[];
newRow.push(1);
for (k=1;k<i;k++)
newRow.push(pascal[i-1][k-1]+pascal[i-1][k]);
newRow.push(1);
pascal.push(newRow);
}
return pascal[n]
}
```
• ###### 8. Re: Titling the entries of array of array

I would put it just put it

It was only to highlight the particular order of pathpoint points and not framed as "actual code".

• ###### 9. Re: Titling the entries of array of array

Hi Saeed,

here a sample appliction for your pascal number generator:

```var n=10;
var a=pascalNumbers(n);

(
{
documentPreferences :
{
pageOrientation : PageOrientation.LANDSCAPE ,
facingPages : false ,
pageWidth : "297mm" ,
pageHeight : "210mm"
}
}
);

(
{
geometricBounds : [ "20mm" , "20mm" , "190mm" , "277mm" ] ,
appliedObjectStyle : "None" ,
contents : a.join("\r")+"\r"
}
);

var story = textFrame.parentStory;

story.texts[0].justification = Justification.CENTER_ALIGN ;

// Convert contents to tables:
story.texts[0].paragraphs.everyItem().texts.everyItem().convertToTable( "," , "\r" );

// Removing empty rows:
story.tables.everyItem().rows[-1].remove();

// Uniform width of every cell in every table of the story:
story.tables.everyItem().cells.everyItem().width = textFrame.parentStory.tables[-1].cells[0].width;

function pascalNumbers(n)
{
var pascal = [];
var row0 = [ 1 ];
var row1 = [ 1,1 ];
var i , newRow , k , ;

pascal.push(row0);
pascal.push(row1);

for ( i=2; i<n+1; i++)
{
newRow=[];
newRow.push(1);

for (k=1;k<i;k++)
{
newRow.push(pascal[i-1][k-1]+pascal[i-1][k]);
}

newRow.push(1);
pascal.push( newRow );
}

return pascal
};

/*

1
1,1
1,2,1
1,3,3,1
1,4,6,4,1
1,5,10,10,5,1
1,6,15,20,15,6,1
1,7,21,35,35,21,7,1
1,8,28,56,70,56,28,8,1
1,9,36,84,126,126,84,36,9,1
1,10,45,120,210,252,210,120,45,10,1

*/
```

Thanks,
Uwe

• ###### 10. Re: Titling the entries of array of array

Hi Laubender,

```var doc = app.documents.add
(
{
documentPreferences :
{
pageOrientation : PageOrientation.LANDSCAPE ,
facingPages : false ,
pageWidth : "297mm" ,
pageHeight : "210mm"
}
}
);

(
{
geometricBounds : [ "20mm" , "20mm" , "190mm" , "277mm" ] ,
appliedObjectStyle : "None" ,
contents : a.join("\r")+"\r"
}
);

```

i run your code but line 14 not working

i tried to write lines 14-21 on one single line but still not working,

what is the problem with my InDesign

• ###### 11. Re: Titling the entries of array of array

Did you copy all lines of my code?

In my code line 14 is doing something else.

It's a closing curly brace.

Maybe you missed it, but function pascalNumbers() now returns the whole array.
And the result—array a—is needed to feed contents of the text frame.

So you have to do the two lines:

var n=10; and var a=pascalNumbers(n);

before everything else.

Regards,
Uwe

• ###### 12. Re: Titling the entries of array of array

i am very sorry YOU ARE correct,

• ###### 13. Re: Titling the entries of array of array

Hi Laubender,

actually i would like to insert the pascal numbers in hexagon .

your replay Gide me to begin from zero.

onotherhand BEFORE your replay i start firs thing manually then i call other script.

this my work till now

```
numberOfObjects=numberOfObjectAtTheBase();
var objectsArray=[]

objectsArray.push(app.selection[0]);

for(i=numberOfObjects-12;i>0;i--)
{
}
pascalNumbers();

//===============================================
function numberOfObjectAtTheBase(){
object=app.selection[0];
a=object.paths[0].pathPoints[0].anchor ;
b=object.paths[0].pathPoints[4].anchor ;
c=moveArray(a,b);
myDocument=app.activeDocument;
myBounds = myDocument.pages[0].bounds;
numberOfObjects=Math.floor((myBounds[2]-myBounds[0])/c[0]);
return numberOfObjects;
}
//==========================================
object=app.selection[0];
a=object.paths[0].pathPoints[2].anchor ;
b=object.paths[0].pathPoints[4].anchor ;
c=moveArray(a,b);
newobject=object.duplicate(undefined, c);
objectsArray.push(newobject);
newobject.select();
}
//=========================================
firstObjectInTheRow=app.selection[0];
for (i=1;i<numberOfObjects;i++){
object=app.selection[0];

a=object.paths[0].pathPoints[0].anchor ;
b=object.paths[0].pathPoints[4].anchor ;
c=moveArray(a,b);
newobject=object.duplicate(undefined, c);
objectsArray.push(newobject);
newobject.select();
}
firstObjectInTheRow.select();
}
//================================================
function moveArray(a,b){
x1= a[0]
x2= b[0]
y1=a[1]
y2= b[1]
x=x2-x1
y=y2-y1
return [x,y]
}
//===========================================
function objectIndex(){
myDocument=app.activeDocument;
myPage=myDocument.pages[0];
myPageItems=myPage.allPageItems;
myPageItems[-1].select();
for (i=objectsArray.length-1;i>-1;i--){
objectsArray[i].select();
// insertPascalNumber(i);
//objectsArray[i].contents=i
}
}
//===================================
function pascalNumbers(){
m=objectsArray.length;
N=numberOfRowsInPascalTriangle(m);
// fill the top cell
crruntcells=objectsArray.length-1;
objectsArray[crruntcells].select();
insertPascalNumber(1);
// fill the secon row
crruntcells-=1;
objectsArray[crruntcells].select();
insertPascalNumber(1);

crruntcells-=1;
objectsArray[crruntcells].select();
insertPascalNumber(1);

// fill the rest of rows
lastRow=[1,1];
crruntRow=[];
for (i=3;i<N+1;i++){
// fill the first cell in the row i
crruntcells-=1;
objectsArray[crruntcells].select();
insertPascalNumber(1);
crruntRow.push(1);
// fill the rest except the last
for (j=1;j<i-1;j++)
{
pascal=lastRow[j-1]+lastRow[j]
crruntRow.push(pascal);
crruntcells-=1;
objectsArray[crruntcells].select();
insertPascalNumber(pascal);
}
// fill in the last
crruntcells-=1;
objectsArray[crruntcells].select();
insertPascalNumber(1);
crruntRow.push(1);
// reinitialize the rows
lastRow.splice(0)
for (k=0;k<crruntRow.length;k++)
lastRow.push(crruntRow[k]);
crruntRow.splice(0);
}
}
//==================================================
function numberOfRowsInPascalTriangle(N)
{
i=1;
count = 0;
while( N>0)
{
count+=1;
N-=i;
i+=1;
}
return count;
}
//========================================

function insertPascalNumber(n){
myPolygon=app.selection[0];
myPolygonTextFrame.visibleBounds=myPolygon.visibleBounds;
myPolygonTextFrame.textFramePreferences.verticalJustification = VerticalJustification.CENTER_ALIGN;
myPolygonTextFrame.contents=(n).toFixed();
myPolygon.textFrames[0].paragraphs[0].
applyParagraphStyle(app.activeDocument.paragraphStyles.item("Number",true));
}
//=====================================
//Store value:
var storedPreset = app.documentPresets[0].createPrimaryTextFrame;

//Let's set it to true:
app.documentPresets[0].createPrimaryTextFrame = true;

//Add the document, name the master as you wish etc.pp. :
(

{
documentPrefreneces :
{
facingPages : false
,
pageWidth : "210 mm"
,
pageHeight : "297 mm"

}
}

);
//Reset to stored value:
app.documentPresets[0].createPrimaryTextFrame = storedPreset;
}

//================================================
//Given a page "myPage", create a new polygon at the bottom left corner
//the size is 10.5mm W × 21mm H...
myPage=app.activeDocument.pages[0]
//Rotate a polygon "myPolygon" around its center point.
var myRotateMatrix =
myPolygon.transform(CoordinateSpaces.pasteboardCoordinates,
AnchorPoint.centerAnchor, myRotateMatrix);
myPolygon.geometricBounds=[263,12.7,284,23.2]
myPolygon.select();
}
//===================================================
```

I have issues on this

first the function on line 24 does not calculate polygon exactly so i do tolerance that number.

//================================

//================================

• ###### 14. Re: Titling the entries of array of array

Hi Laubender,

many thanks to you and all people in this forum who give help.

you gave me an idea of application i am looking for.This application working well except for the prime number.

Here is the application

```//add document
// calculate the number of polygon at the bottom of the page
numberOfObjects=numberOfObjectAtTheBase();
//  objectsArray is to contain polygon from bottom in order up
var objectsArray=[];
objectsArray.push(app.selection[0]);
//add the polygon starting from the bottom
for(i=numberOfObjects-19;i>0;i--)
{
}
//insert the pascal in the polygons
pascalNumbers();
// color the polygon one some criteria such as color odd numbers , even numbers , prime number....
colorPascal();

//===============================================
function numberOfObjectAtTheBase(){
object=app.selection[0];
a=object.paths[0].pathPoints[0].anchor ;
b=object.paths[0].pathPoints[4].anchor ;
c=moveArray(a,b);
myDocument=app.activeDocument;
myBounds = myDocument.pages[0].bounds;
numberOfObjects=Math.floor((myBounds[2]-myBounds[0])/c[0]);
return numberOfObjects;
}
//==========================================
object=app.selection[0];
a=object.paths[0].pathPoints[2].anchor ;
b=object.paths[0].pathPoints[4].anchor ;
c=moveArray(a,b);
newobject=object.duplicate(undefined, c);
objectsArray.push(newobject);
newobject.select();
}
//=========================================
firstObjectInTheRow=app.selection[0];
for (i=1;i<numberOfObjects;i++){
object=app.selection[0];

a=object.paths[0].pathPoints[0].anchor ;
b=object.paths[0].pathPoints[4].anchor ;
c=moveArray(a,b);
newobject=object.duplicate(undefined, c);
objectsArray.push(newobject);
newobject.select();
}
firstObjectInTheRow.select();
}
//================================================
function moveArray(a,b){
x1= a[0]
x2= b[0]
y1=a[1]
y2= b[1]
x=x2-x1
y=y2-y1
return [x,y]
}
//===========================================
function objectIndex(){
myDocument=app.activeDocument;
myPage=myDocument.pages[0];
myPageItems=myPage.allPageItems;
myPageItems[-1].select();
for (i=objectsArray.length-1;i>-1;i--){
objectsArray[i].select();
// insertPascalNumber(i);
//objectsArray[i].contents=i
}
}
//===================================
function pascalNumbers(){
m=objectsArray.length;
N=numberOfRowsInPascalTriangle(m);
// fill the top cell
crruntcells=objectsArray.length-1;
objectsArray[crruntcells].select();
insertPascalNumber(1);
// fill the secon row
crruntcells-=1;
objectsArray[crruntcells].select();
insertPascalNumber(1);
crruntcells-=1;
objectsArray[crruntcells].select();
insertPascalNumber(1);
// fill the rest of rows
lastRow=[1,1];
crruntRow=[];
for (i=3;i<N+1;i++){
// fill the first cell in the row i
crruntcells-=1;
objectsArray[crruntcells].select();
insertPascalNumber(1);
crruntRow.push(1);
// fill the rest except the last
for (j=1;j<i-1;j++)
{
pascal=lastRow[j-1]+lastRow[j]
crruntRow.push(pascal);
crruntcells-=1;
objectsArray[crruntcells].select();
insertPascalNumber(pascal);
}
// fill in the last
crruntcells-=1;
objectsArray[crruntcells].select();
insertPascalNumber(1);
crruntRow.push(1);
// reinitialize the rows
lastRow.splice(0)
for (k=0;k<crruntRow.length;k++)
lastRow.push(crruntRow[k]);
crruntRow.splice(0);
};
}
//==================================================
function numberOfRowsInPascalTriangle(N){
i=1;
count = 0;
while( N>0)
{
count+=1;
N-=i;
i+=1;
};
return count;
}
//========================================
function insertPascalNumber(n){
myPolygon=app.selection[0];
myPolygonTextFrame.visibleBounds=myPolygon.visibleBounds;
myPolygonTextFrame.textFramePreferences.verticalJustification = VerticalJustification.CENTER_ALIGN;
myPolygonTextFrame.contents=(n).toFixed();
myPolygon.textFrames[0].paragraphs[0].
applyParagraphStyle(app.activeDocument.paragraphStyles.item("Number",true));
}
//=====================================
//Store value:
var storedPreset = app.documentPresets[0].createPrimaryTextFrame;
//Let's set it to true:
app.documentPresets[0].createPrimaryTextFrame = true;
//Add the document, name the master as you wish etc.pp. :
(

{
documentPrefreneces :
{
facingPages : false,
pageWidth : "210 mm",
pageHeight : "297 mm"
}
}

);
//Reset to stored value:
app.documentPresets[0].createPrimaryTextFrame = storedPreset;
}

//================================================
//Given a page "myPage", create a new polygon at the bottom left corner
//the size is 10.5mm W × 21mm H...
//or the size is 6mm W × 12mm H..
myPage=app.activeDocument.pages[0]
//Rotate a polygon "myPolygon" around its center point.
var myRotateMatrix =
myPolygon.transform(CoordinateSpaces.pasteboardCoordinates,
AnchorPoint.centerAnchor, myRotateMatrix);
//myPolygon.geometricBounds=[263,12.7,275,18.7]
myPolygon.geometricBounds=[ 272.25,12.7,284.25,18.7]
myPolygon.select();
}
//===================================================
function colorPascal(){
myDocument=app.activeDocument
myPage=myDocument.pages[0]
myPolygons=myPage.polygons;
for ( i=0;i<myPolygons.length;i++)
if (isEven(Number(myPolygons[i].textFrames[0].contents)))
//if (is3multiple(Number(myPolygons[i].textFrames[0].contents)))
//if (is5multiple(Number(myPolygons[i].textFrames[0].contents)))
//if (is7multiple(Number(myPolygons[i].textFrames[0].contents)))
//if (isPrime(Number(myPolygons[i].textFrames[0].contents)))
myPolygons[i].textFrames[0].fillColor="C=15 M=100 Y=100 K=0";
}
//====================================================
function isEven(anyInteger){
if(Math.floor(anyInteger/2)*2==anyInteger)
return true;
else
return false;
}
//==============================================
function is3multiple(anyInteger){
if(Math.floor(anyInteger/3)*3==anyInteger)
return true;
else
return false;
}
//==================================
function is5multiple(anyInteger){
if(Math.floor(anyInteger/5)*5==anyInteger)
return true;
else
return false;
}
//===================
function is7multiple(anyInteger){
if(Math.floor(anyInteger/7)*7==anyInteger)
return true;
else
return false;

}
//===================
function isPrime(anyInteger){
if (anyInteger==1)
return false;
if(anyInteger==2||anyInteger==3)
return true;
else
for (i=2;i<=Math.floor(anyInteger/2);i++)
if(Math.floor(anyInteger/i)*i==anyInteger)
return false;
else
isPrime=true;
return isPrime;
}
```

it produce the flowing

• ###### 15. Re: Titling the entries of array of array

hi Saeedfs, (post 9 de Laubender)

pour moi il y juste une petit erreur là (ligne 45) :

``` var i , newRow , k, ;
```

correction :

``` var i , newRow , k;
```

A+

• ###### 16. Re: Titling the entries of array of array

That's my error…

Regards,
Uwe

• ###### 17. Re: Titling the entries of array of array

Yes, il est compliqué de faire suivre les postes .... (un détail).

Merci  à vous tous, lire tous ces postes apporte beaucoup à ma compréhension des script.

Regards

Philippe