How to iterate through a collection from end to start

Zsolt wrote on 5/28/2004, 10:21 PM
Hello,
I would like to iterate through a collection of track events from end to
start but couldn't do it. Can somebody explain this to me ?

On a side not seems like Vegas is not referencing events by reference
but in a (track,event) matrix....quite surprising to me.
Is this normal ?

Thanks in advance:
Zsolt

Comments

jetdv wrote on 5/29/2004, 1:34 PM
for (ei=0; ei <= track.Events.Count - 1; ei++) {
evnt = TrackEvent(track.Events[ei]);
//process evnt
}
Zsolt wrote on 5/29/2004, 2:08 PM
Yes,
but wouldn't this iterate through the event srom start to end ?
What I want to do is if I have 600 events on a track then process
the 600th first, then the 599th, then the 598th and so on...
If I understand correctly your code will process events in order
0 -> 1-> 2 -> .... 599.
Regards:
Zsolt
jetdv wrote on 5/29/2004, 9:21 PM
Then reverse the FOR loop! Have it START at track.Events.Count - 1 and end a zero decreasing each time.

for (ei=track.Events.Count - 1; ei > 0; ei--) {
Zsolt wrote on 5/31/2004, 3:50 AM
Ok, but there is only the moveNext() method I know of for enumerations
but not movePrevious() or something.
Neverthless I solved the problem by making an array and use the array's
reverse() method :)))
Thanks:
Zsolt
jetdv wrote on 5/31/2004, 3:46 PM
If you use my FOR loop, you don't NEED a "move" command. It iterates through the events JUST LIKE they are an array.
Zsolt wrote on 6/1/2004, 3:15 AM
Wow, thanks, I didn't knew that.
I'm new to JScript and the scripting documentation doesn't mention things like
this.
Regards:
Zsolt
dust wrote on 6/16/2004, 11:20 PM
Be careful though you don't actually change elements in the container (track) within the loop body, like removing events from the track while looping through all events of a track, or moving them to another track. This would break your loop. In such a case, you must first put all events you want to loop through into another container and then loop through this one (which is not so inefficient because it is all done "by reference". So, for example:

...
import System.Collections;
...
var queue = new Queue; // All selected video events

for (var e in track.Events)
{ if (e.Selected) queue.Enqueue(e); }

for (var e in queue) { .... }

Note that this would loop events in normal order (1...x). To reverse the order, use "Stack" instead of "Queue" and "Push" instead of "Enqueue".
SonyPJM wrote on 6/18/2004, 7:55 AM
Thanks for making this point... you are correct.

Events are sorted by start time. When you access them by index or
using an enumerator, any changes to that effect the sort order will
also change the index and enumeration order.

So for example, to delete every other event in a track you'd do
something like this:


var events : Events = Vegas.Project.Tracks[0].Events;
var eventsToDelete : ArrayList = new ArrayList();
var enumEvents : IEnumerator = events.GetEnumerator();
var toggle : Boolean = false;
while (enumEvents.MoveNext()) {
if (toggle) {
eventsToDelete.Add(enumEvents.Current);
toggle = false;
} else {
toggle = true;
}
}

enumEvents = eventsToDelete.GetEnumerator();
while (enumEvents.MoveNext()) {
events.Remove(enumEvents.Current);
}