Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions loop thru an array to find and save the higher of compared numbers

  • loop thru an array to find and save the higher of compared numbers

    Posted by Chris Gulliford on September 4, 2014 at 8:34 pm

    I need to create a loop that will compare each number in an array to a stored variable (that starts with 0) to determine which value is higher, replace that stored variable if the new array value is higher, and then display the stored value. Any help would be appreciated.

    Kevin Camp replied 11 years, 11 months ago 3 Members · 7 Replies
  • 7 Replies
  • Dan Ebberts

    September 4, 2014 at 9:31 pm

    I’m not sure what you mean by “starts with 0”, and I’m curious what “replace that stored variable” is all about (since variables don’t survive from frame to frame). More detail about what you’re trying to do would be helpful (more so than how you’re trying to do it).

    Dan

  • Chris Gulliford

    September 4, 2014 at 9:57 pm

    Thanks for the response. With some help I’ve created a visible number that displays each number of an array in succession. The array is a comma delimited list pasted into a text layer. The visible number is an expression in a second layer that displays each number. To look at it the number goes up and down like a car digital speedometer. What I need to do now is create a second expression that keeps track of and dislays each highest number as it succeeds. Ex: Array #1 is higher than 2 so 1 displays. 3 is higher that 2, but lower than 1 so 1 remains…etc. your help is appreciated.

  • Kevin Camp

    September 4, 2014 at 10:09 pm

    i think this does what you are asking:

    myArray = [1,2,3,4,5,6,7,8,9];
    n=0;
    for (i=0; i<myArray.length; i++){
    n=Math.max(n,myArray[i]);
    }
    n

    it will loop through every value in the array and return the highest value or zero, whichever is higher.

    Kevin Camp
    Art Director
    KCPQ, KZJO & KRCW

  • Kevin Camp

    September 4, 2014 at 10:28 pm

    i was writing my reply as you were writing yours… but i don’t think my original reply will work for what you want.

    as dan mentioned earlier, you can’t pass (or store) a variable from frame to frame, so i think you’d need to re-parse the array with the same timing as how you are parsing it in the fluctuating text layer, and display the greatest value up to that point in time…

    so say your values are changing every frame, you could use an expression similar to the one i provided to count through your array for every frame up to the current frame and determine the max value to that point.

    we could do an expression like that for you, but it would help to know how the values are currently changing

    Kevin Camp
    Art Director
    KCPQ, KZJO & KRCW

  • Dan Ebberts

    September 4, 2014 at 10:49 pm

    Something like this should give the text representation of the highest number in your text array (I’m assuming your text layer looks something like this: -1,1,2,3.03,-7):


    arr = thisComp.layer("Array Layer").text.sourceText.split(",");
    for (i = 0; i myMax){
    myMax = n;;
    iSave = i;
    }
    }
    arr[iSave]

    But your text data would have to be animated for the max value to change.

    Dan

    Edit–Oops, something happend to the code when I pasted it in. Should be fixed now.

  • Chris Gulliford

    September 5, 2014 at 12:44 pm

    Folks, thanks for your help… both operations you all successfully provide the highest number in the list. That is not exactly what I’m shooting for. Below is the code I have that prints to screen (for lack of the correct terminology) the numbers of my array at a rate of 5 times per second (every sixth frame/30 fps).

    see code

    So… what needs to happen is an incremental comparison with the highest number up to that point being displayed.

    example:

    01 0 – 0 is displayed
    02 35 – 35 is displayed
    03 100 – 100 is displayed
    04 95 – 100 remains displayed
    05 125 – 125 is displayed
    06 100 – 125 remains displayed

    thanks for all your efforts.

    // expression to take input of series of numerical values and display them five per second or every six frames
    // get our source list
    listText = thisComp.layer("heart").text.sourceText;

    // now make an array, assuming a comma delimiter
    list = listText.split(",");

    // what's the biggest value? we'll use this as our denominator
    maximum = Math.max.apply(Math, list);

    // which number should we look at? one per six frames
    wholeSeconds = Math.floor(time-inPoint);
    milliSeconds = Math.floor(((time-inPoint) - wholeSeconds) * 1000);
    indexModifier = Math.floor(milliSeconds/200);
    target = (wholeSeconds*5) + indexModifier;

    // make sure we don't try to find the value-as-a-percentage for an out-of-bounds array number, or try to divide by zero
    if ((target >= 0) && (target &lt; list.length) && (maximum != 0)) {

    // this will print to screen the array number or zero
    speed = list[target];
    } else speed= 0;

  • Kevin Camp

    September 5, 2014 at 9:34 pm

    duplicate your text layer and then add this to the end of your expression:

    n = 0;
    for (i = 0; i <= target && i < list.length; i++){
    n = Math.max(n, list[i]);
    }
    n

    Kevin Camp
    Art Director
    KCPQ, KZJO & KRCW

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy