🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: adobe.scripting.indesign
13 messages
13 total messages Started by Dave_Saunders Tue, 31 Mar 2009 14:44
Using for ... in with arrays
#184
Author: Dave_Saunders
Date: Tue, 31 Mar 2009 14:44
15 lines
993 bytes
Is it obvious to everybody that:

var myArray = [3,4,5] 
for (j in myArray) { 
  $.writeln(j); 
}

produces in the JavaScript Console:

0
1
2
Execution finished.

I stumbled on a script yesterday that I wrote a while back that used this technique for iterating through an array but I had to step back and try this test to convince myself that it actually works.
Re: Using for ... in with arrays
#190
Author: Dave_Saunders
Date: Tue, 31 Mar 2009 15:51
13 lines
933 bytes
Fred,

Right. It was that realization that had it making sense for me, once I'd thought it through.

I wonder which is quicker:

for (var j in myArray)

for (var j = 0; myArray.length > j; j++)

I appreciate that these aren't necessarily equivalent, but every time I use the latter, I surely could have used the former.

Dave
Re: Using for ... in with arrays
#191
Author: Bob Stucky
Date: Tue, 31 Mar 2009 16:24
18 lines
1295 bytes
There is a difference between the two. In the second example below, i is a string, not an int. (put a debugger statement in there and do typeof( i ); in the console to prove it). In the second example we're asking for the array index of:
"01", "11", "12"...

And they're all undefined.

I NEVER use the for( var i in array ) syntax for that reason. If you do introduce a bug there, it's one of those bugs that your eye doesn't catch, and takes forever to find.

Bob

var array = [0,1,2,3,4,5,6,7,8,9];

for ( var i = 0; i < array.length - 1; i++ ) {
	$.writeln( array[ i + 1 ] );
}

for ( var i in array ) {
	$.writeln( array[ i + 1 ] );
}
Re: Using for ... in with arrays
#192
Author: Fred_Goldman
Date: Tue, 31 Mar 2009 16:39
7 lines
1027 bytes
But why doesn't something like this work?

Isn't myDoc.characterStyles an array?

myDoc = app.activeDocument;
for (j in myDoc.characterStyles)
   alert(myDoc.characterStyles[j].name)
Re: Using for ... in with arrays
#195
Author: Fred_Goldman
Date: Tue, 31 Mar 2009 16:42
5 lines
898 bytes
You could do this:

for ( var i in array ) {
	$.writeln( array[ Number(i) + 1 ] );
}
Re: Using for ... in with arrays
#196
Author: Dave_Saunders
Date: Tue, 31 Mar 2009 16:44
3 lines
620 bytes
Ah, thanks Bob.

Dave
Re: Using for ... in with arrays
#198
Author: Bob Stucky
Date: Tue, 31 Mar 2009 17:10
3 lines
764 bytes
Exactly, Fred. That's why I never use the var i in array syntax. There's just too many possibilities for subtle errors.

Bob
Re: Using for ... in with arrays
#199
Author: xbytor
Date: Tue, 31 Mar 2009 17:16
5 lines
990 bytes
 > Isn't myDoc.characterStyles an array?

There are 'Collection' objects in Adobe/JS-land that look kinda like Arrays but 
really aren't. It would have been very nice if they would have made these 
collection objects proper subclasses of Array.
Re: Using for ... in with arrays
#200
Author: Fred_Goldman
Date: Tue, 31 Mar 2009 17:30
3 lines
1068 bytes
I see, so collections don't support the for/in loop as arrays do.

Is there anything else arrays support that collections don't?
Re: Using for ... in with arrays
#3
Author: Dave_Saunders
Date: Tue, 31 Mar 2009 17:49
15 lines
902 bytes
Because collections are objects, they do support for/in loops, but the results aren't very useful:

myDocs = app.documents; 
for (j in myDocs) { 
   $.writeln(j); 
}

produces:

length 
Execution finished.

Which indicates that a collection has just one (visible) property named length.

Dave
Re: Using for ... in with arrays
#201
Author: Olav_Kvern
Date: Tue, 31 Mar 2009 17:50
11 lines
1923 bytes
xbytor wrote: "There are 'Collection' objects in Adobe/JS-land that look kinda like Arrays but really aren't. It would have been very nice if they would have made these collection objects proper subclasses of Array."

That won't work for all of the languages we support. In VBScript, for example, these collections give us capabilities that we would not otherwise have (ItemByName, ItemByRange, etc.). While we could extend Array in JavaScript, we can't do that in the other languages (or, at least, we can't do so given our current process). There are a lot of things that our collections support that arrays don't (except in AppleScript, where arrays and collections work about the same way--in fact, our collections are based on the AppleScript model).

Our "one size fits all" approach does sometimes mean that we can't take advantage of all of the features of a specific language, but it's better than the alternatives--hand coding for each language you want to support (look at some of the other CS applications, which have different DOMs for differing languages), or limiting scripting to a single language.

This is also why I always use the collection.item(index) form when working with these collections and the array[index] form for arrays. This reminder makes it much easier for me when I'm updating scripts.

Thanks,

Ole
Re: Using for ... in with arrays
#189
Author: Fred_Goldman
Date: Tue, 31 Mar 2009 22:46
10 lines
977 bytes
It makes sense, because an array is an object and the properties of the 
array are the index number of the array element.

Try this:

var myArray = new Array(50)
myArray[25] = 3
for (j in myArray) {
   $.writeln(j);
}
Re: Using for ... in with arrays
#197
Author: Fred_Goldman
Date: Wed, 01 Apr 2009 00:03
17 lines
1417 bytes
It seems that myDoc.characterStyles is a regular object with a length 
property added to it, not an actual array.

Another major difference between the for/in and traditional loops is 
that the for/in loop will not loop through the full length of the array 
if it has undefined items whereas the traditional for loop will.

For example:

myDoc = app.activeDocument;
myArray = new Array(myDoc.characterStyles.length)
myArray[3] = 4;
for (j in myArray)
$.writeln(j)

Will only loop once despite the fact that there may be more than one 
character style in the document.
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads