Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Check to see if var is in array

  • Check to see if var is in array

    Posted by Edward Clayton on July 14, 2022 at 8:10 pm

    I’m working on an expression to determine the type (lowercase, uppercase, number, symbol) of the last character in a string.

    Right now I’m doing something like this

    par = parent.text.sourceText;

    src = par.charAt(par.length-1);

    lib = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];

    libNum = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "/", ":", ";", "(", ")", "$", "&", "@", '"', '“', '”', ".", ",", "?", "!", "'", "’"];

    libSym = ["[", "]", "{", "}", "#", "%", "^", "*", "+", "=", "_", "\\", "|", "~", "<", ">", "€", "£", "¥", "•"];

    if (src == " ") {

    1

    } else {

    for (i=0;i<lib.length;i++) {

    if (src == lib[i]) {

    2

    break

    } else if (src == lib[i].toUpperCase()) {

    3

    break

    } else {

    for (n=0;n<libNum.length;n++) {

    if (src == libNum[n]) {

    4

    break

    } else {

    for (s=0;s<libSym.length;s++) {

    if (src == libSym[s]) {

    5

    break

    }

    }

    }

    }

    }

    }

    }

    This technically works, but I was wondering if there is a more efficient way to do this. This is quite slow as it requires nesting for loops within for loops.

    Michael Hunter replied 3 years, 10 months ago 3 Members · 3 Replies
  • 3 Replies
  • Dan Ebberts

    July 14, 2022 at 8:26 pm

    If you’re using the JavaScript expression engine, you can improve things a lot by using array.includes():

    par = parent.text.sourceText;
    src = par.charAt(par.length-1);
    lib = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];
    libNum = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "/", ":", ";", "(", ")", "$", "&", "@", '"', '“', '”', ".", ",", "?", "!", "'", "’"];
    libSym = ["[", "]", "{", "}", "#", "%", "^", "*", "+", "=", "_", "\\", "|", "~", "<", ">", "€", "£", "¥", "•"];
    if (src == " ") {
    1;
    }else if (lib.includes(src)){
    2;
    }else if (lib.includes(src.toLowerCase())){
    3;
    }else if (libNum.includes(src)){
    4;
    }else if (libSym.includes(src)){
    5;
    }else{ // just in case
    6;
    }
  • Edward Clayton

    July 14, 2022 at 9:10 pm

    Perfect, thank you!

  • Michael Hunter

    July 14, 2022 at 11:00 pm

    I like dan’s code. If distinct you can also create a strings a libraries and switch on charindex(input)

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