FORUMS: list search recent posts

# If/Else Conditional with an Array

FAQ   •   VIEW ALL
 If/Else Conditional with an Array on May 16, 2017 at 1:03:53 pm

Hi Guys. I've worked out a solution for my problem, but I feel it's convoluted and unnecessarily clumsy. I stumbled across whispers and shadows of being able to use an array with If/Else conditional statements, but haven't found anything that really helped me learn more. I'm hoping someone here could shed some light on this for me.

Here's the story...

I have a "statistics box" made up of multiple columns and rows. The largest configuration is 5 rows x 3 columns. The smallest configuration is 1 row x 1 column. I have one text layer named "Number of Stats Categories" and another text layer named "Number of Columns." I use the input on these two text layers to dynamically determine the size and position of the "statistics box."

Below is the expression I have on Position. It's basically just a bunch of If/Else statements. One If/Else statement for every possible configuration between 5x3 and 1x1. So...if 5x3, position = [x1,y1] ... if 5x2, position = [x2,y2], etc, etc etc.

As you can see below my expression is pretty darn long. That being said it is working really well. It's doing exactly what I wanted it to do. However, I feel there is probably a better way to do this and I'd love to learn of a new, better method if it's out there.

I've added some images to this post as well so you can see an example of the dynamic scaling and positioning based on the input for Number of Stats Categories and Number of Columns. The images are for a 5x3 stats box and a 3x2 stats box.

``` StatsCats = thisComp.layer("Number of Stats Categories").text.sourceText; NumCol = thisComp.layer("Number of Columns").text.sourceText; if (StatsCats == 5 && NumCol == 3) { [1300,575]; } else if (StatsCats == 5 && NumCol == 2) { [1550,575]; } else if (StatsCats == 5 && NumCol == 1) { [1700,575]; } else if (StatsCats == 4 && NumCol == 3) { [1300,635]; } else if (StatsCats == 4 && NumCol == 2) { [1550,635]; } else if (StatsCats == 4 && NumCol == 1) { [1700,635]; } else if (StatsCats == 3 && NumCol == 3) { [1300,695]; } else if (StatsCats == 3 && NumCol == 2) { [1550,695]; } else if (StatsCats == 3 && NumCol == 1) { [1700,695]; } else if (StatsCats == 2 && NumCol == 3) { [1300,740]; } else if (StatsCats == 2 && NumCol == 2) { [1550,740]; } else if (StatsCats == 2 && NumCol == 1) { [1700,740]; } else if (StatsCats == 1 && NumCol == 3) { [1300,795]; } else if (StatsCats == 1 && NumCol == 2) { [1550,795]; } else if (StatsCats == 1 && NumCol == 1) { [1700,795]; } else { [1300,575]; } ```

Sample Images

 Re: If/Else Conditional with an Arrayon May 16, 2017 at 1:34:45 pm

Something like this maybe:

StatsCats = parseInt(thisComp.layer("Number of Stats Categories").text.sourceText,10);
NumCol = parseInt(thisComp.layer("Number of Columns").text.sourceText,10);
xArr = [1700,1550,1300];
yArr = [795,740,695,635,575];
x = xArr[NumCol-1];
y = yArr[StatsCats-1];
[x,y]

Dan

 Re: If/Else Conditional with an Arrayon May 16, 2017 at 1:50:48 pm

Hi Dan. As always, simply amazing!

If you don't mind me asking. What exactly is going on in that expression? I'd love to learn what you did there so I can add it to my knowledge base and try to apply that method to similar problems in the future.

Thank you very much.

 Re: If/Else Conditional with an Arrayon May 22, 2017 at 2:47:26 pm

Hi Dan. Thanks again for the help with this. I was wondering if you could help me break it down so I know what is going on and be able to apply it to future expressions. Thanks!

 Re: If/Else Conditional with an Arrayon May 22, 2017 at 6:58:10 pm

It's hard to know where to start without knowing how much you know about JavaScript arrays. If you're unfamiliar with them, you might want to pick up a good JavaScript reference. In any case, in your example, the x position is one of three choices, depending on the number of columns, and the y position is one of five choices depending on the number of rows. So I put those choices into two arrays, and use the number of columns, and number of rows as indexes into their respective arrays.

Since JavaScript array indices are zero-based, I had to subtract one from each index.

That's really all that's going on (except for parseInt, which just converts text strings to integers).

I hope that helps.

Dan

 Re: If/Else Conditional with an Arrayon May 16, 2017 at 1:41:38 pm

Hi,

I haven't tested it, but I think this should work too:

StatsCats = thisComp.layer("Number of Stats Categories").text.sourceText;
NumCol = thisComp.layer("Number of Columns").text.sourceText;

if(StatCats == 1 || StatCats == 3){
c = 855;
}else{
c= 860;
};

[1950 - (NumCol * 250), c - (StatsCats * 60)];

Dan's solution is way more flexible, though !
Cheers !;)