Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions For Loop to search an array

Tagged: 

  • For Loop to search an array

    Posted by Mike Ridolfi on October 14, 2020 at 11:53 pm

    I have a source text on another layer where a user can input values separated by semi-colon:

    a = [10; 20; 30; 40]

    I need to check this array to see if any value is above my maximum number (“cap”)

    However, when I use a for loop expression, it’s only looking at the last number in my array and not considering if any other value is above my cap:

    a = thisComp.layer(“Text_Input”).text.sourceText.split(“; “);

    l = a.length;

    cap = 100;

    for (i=0; i<l; i++){

    if(a[i] >= cap) “Fail”; else “Pass”

    }

    I feel like I’m missing something really obvious here.

    Brendon Murphy replied 5 years, 7 months ago 2 Members · 4 Replies
  • 4 Replies
  • Brendon Murphy

    October 15, 2020 at 3:01 pm

    You have a few issues here. The main one is the difference between AE seeing the text input as a string vs integers. To split it by “;” it needs to be a string. To compare it to 100, the numbers need to be integers. Also, you need closing braces within your if statement, and to call for an alert with your “pass” or “fail”. For a script, it’s like this:

    var thisComp = app.project.activeItem;

    var a = thisComp.layer(“Text_Input”).text.sourceText.value; //look at the value of the source text

    var aString = a.toString(); //convert value to string

    var aArray = aString.split(‘;’);//split it

    var l = aArray.length;

    var cap = 100;

    for (i=0; i<l; i++){<=”” p=””></l;>

    //convert the array values into integers as you cycle through them

    if(parseInt(aArray[i]) >= cap) {

    alert(“Fail”);

    }else {

    alert(“Pass”);

    };

    };

    Expression version is below.

  • Brendon Murphy

    October 15, 2020 at 3:16 pm

    Sorry, just noticed you need this as an expression on another layer. It’s like this:

    a = thisComp.layer(“Text_Input”).text.sourceText.value;

    aString = a.toString();

    aArray = aString.split(‘;’);

    l = aArray.length;

    cap = 100;

    failCheck=[];

    for (i=0; i<l; i++){

    if(parseInt(aArray[i]) >= cap) {

    failCheck.push(“Fail”);

    }else {

    failCheck.push(“Pass”);

    };

    };

    failCheck;

  • Mike Ridolfi

    October 15, 2020 at 5:03 pm

    Oh wow, I was way off base! Thanks so much.

    For anyone who needs to use this, there is a small syntax error on the aArray = aString.split(‘:’), it should be “;”. Also the other “” are coming in as ” ” when you copy into Ae. But once swapped out it works perfectly!

  • Brendon Murphy

    October 15, 2020 at 7:01 pm

    Yeah, I’ve noticed the forum font swaps those quotations. I need to learn how to add the little code window to avoid that!

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