FORUMS: list search recent posts

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

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
chris gulliford
loop thru an array to find and save the higher of compared numbers
on Sep 4, 2014 at 8:34:13 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.


Return to posts index

Dan Ebberts
Re: loop thru an array to find and save the higher of compared numbers
on Sep 4, 2014 at 9:31:04 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



Return to posts index

chris gulliford
Re: loop thru an array to find and save the higher of compared numbers
on Sep 4, 2014 at 9:57:39 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.


Return to posts index


Kevin Camp
Re: loop thru an array to find and save the higher of compared numbers
on Sep 4, 2014 at 10:28:39 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


Return to posts index

Dan Ebberts
Re: loop thru an array to find and save the higher of compared numbers
on Sep 4, 2014 at 10:49:36 pm
Last Edited By Dan Ebberts on Sep 4, 2014 at 10:50:51 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<arr.length; i++){
n = parseFloat(arr[i]);
if (i == 0){
myMax = n;
iSave = i;
}else if (n > 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.



Return to posts index

Kevin Camp
Re: loop thru an array to find and save the higher of compared numbers
on Sep 4, 2014 at 10:09:48 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


Return to posts index


chris gulliford
Re: loop thru an array to find and save the higher of compared numbers
on Sep 5, 2014 at 12:44:14 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;


Return to posts index

Kevin Camp
Re: loop thru an array to find and save the higher of compared numbers
on Sep 5, 2014 at 9:34:44 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


Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2017 CreativeCOW.net All Rights Reserved
[TOP]