Suppose you want to perform multiple actions based on some condition. That is, you want to do something like:
if condition then x = 1 y = 2 z = 3 else x = 4 y = 5 z = 6
Most other programming languages provide if
-else
constructs that accept multiple statements. AviSynth, however, provides only
the ternary operator (?:
), which can be used only
with single expressions. Therefore, the conventional way to
replicate the above control structure would be to split it up:
x = condition ? 1 : 4 y = condition ? 2 : 5 z = condition ? 3 : 6
There are a number of undesirable properties of this method:
What can we do instead? We can (ab)use AviSynth's underappreciated
Eval
function.
Eval("text")
feeds text back
into the script parser to evaluate it, as if you had typed text
by itself (with no quotes) in the script. Eval("text")
and text are equivalent.
Consequently, if text consists of multiple lines,
Eval
will evaluate it as if multiple lines appeared in the script.
This means that
s = "x = 1 y = 2 z = 3" Eval(s)
is equivalent to
x = 1 y = 2 z = 3
Putting it all together, we now can see a way to replicate the structure of
the original if
-else
example:
s = condition ? "x = 1 y = 2 z = 3" \ : "x = 4 y = 5 z = 6" Eval(s)