<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="http://avisynth.nl/skins/common/feed.css?303"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>http://avisynth.nl/index.php?action=history&amp;feed=atom&amp;title=Block_statements</id>
		<title>Block statements - Revision history</title>
		<link rel="self" type="application/atom+xml" href="http://avisynth.nl/index.php?action=history&amp;feed=atom&amp;title=Block_statements"/>
		<link rel="alternate" type="text/html" href="http://avisynth.nl/index.php?title=Block_statements&amp;action=history"/>
		<updated>2026-05-27T16:02:45Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.19.24</generator>

	<entry>
		<id>http://avisynth.nl/index.php?title=Block_statements&amp;diff=742&amp;oldid=prev</id>
		<title>Admin: 1 revision</title>
		<link rel="alternate" type="text/html" href="http://avisynth.nl/index.php?title=Block_statements&amp;diff=742&amp;oldid=prev"/>
				<updated>2013-05-09T21:33:34Z</updated>
		
		<summary type="html">&lt;p&gt;1 revision&lt;/p&gt;
&lt;table class='diff diff-contentalign-left'&gt;
			&lt;tr valign='top'&gt;
			&lt;td colspan='1' style=&quot;background-color: white; color:black;&quot;&gt;← Older revision&lt;/td&gt;
			&lt;td colspan='1' style=&quot;background-color: white; color:black;&quot;&gt;Revision as of 21:33, 9 May 2013&lt;/td&gt;
			&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	<entry>
		<id>http://avisynth.nl/index.php?title=Block_statements&amp;diff=741&amp;oldid=prev</id>
		<title>Unreal666: /* For..Next loop with access to variables in local scope */ succintly -&gt; succinctly</title>
		<link rel="alternate" type="text/html" href="http://avisynth.nl/index.php?title=Block_statements&amp;diff=741&amp;oldid=prev"/>
				<updated>2011-03-30T03:27:13Z</updated>
		
		<summary type="html">&lt;p&gt;‎&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;For..Next loop with access to variables in local scope: &lt;/span&gt; succintly -&amp;gt; succinctly&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== Background ==&lt;br /&gt;
&lt;br /&gt;
A first glance at Avisynth documentation leaves the impression that aside from function definitions, block statements are not possible in Avisynth script. However, there are specific features of the language allowing the construction of block statements that have remained unaltered to date and probably will remain so in the future since block statements are very useful in extending the capabilities of the script language.&lt;br /&gt;
&lt;br /&gt;
Indeed, in most programming and scripting languages, block statements are very useful tools for grouping together a set of operations that should be applied together under certain conditions. They are also useful in Avisynth scripts. &lt;br /&gt;
&lt;br /&gt;
Assume, for example, that after an initial processing of your input video file, you want to further process your input differently (for example, apply a different series of [[Internal_filters|filters]] or apply the same set of filters with different order) based on a certain condition calculated during the initial processing, which is coded at the value of Boolean variable ''cond''.&lt;br /&gt;
&lt;br /&gt;
Instead of making an ugly series of successive conditional assignments using the conditional (ternary) [[Operators|operator]], &amp;lt;tt&amp;gt;?:&amp;lt;/tt&amp;gt;, as in '''Example 1''' below (items in brackets are not needed if you use the implicit ''last'' variable to hold the result):&lt;br /&gt;
&lt;br /&gt;
'''Example 1'''&lt;br /&gt;
 [result_1 = ]cond ? filter1_1 : filter2_1&lt;br /&gt;
 [result_2 = ]cond ? filter1_2 : filter2_2&lt;br /&gt;
 ...&lt;br /&gt;
 [result_n = ]cond ? filter1_n : filter2_n&lt;br /&gt;
&lt;br /&gt;
It would be nice to be able to construct two blocks of filter operations and branch in a single step, as in the (ideal) '''Example 2''' below:&lt;br /&gt;
&lt;br /&gt;
'''Example 2'''&lt;br /&gt;
 [result = ] cond ? {&lt;br /&gt;
     filter1_1&lt;br /&gt;
     filter1_2&lt;br /&gt;
     ...&lt;br /&gt;
     filter1_n&lt;br /&gt;
 } : {&lt;br /&gt;
     filter2_1&lt;br /&gt;
     filter2_2&lt;br /&gt;
     ...&lt;br /&gt;
     filter2_n&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Something approaching this construction (and others) '''is''' possible; perhaps some constraints may apply, but you will nevertheless be capable of providing more powerful flow control to your scripts. The rest of this section will show you how to implement them using standard Avisynth constructs.&lt;br /&gt;
&lt;br /&gt;
(An alternative, and possibly more user-friendly, approach would be to use the external [http://forum.doom9.org/showthread.php?t=147846 GScript] plugin, which extends the Avisynth scripting language to provide multi-line conditionals (if-then-else blocks), 'while' loops and 'for' loops.)&lt;br /&gt;
&lt;br /&gt;
=== Features enabling construction of block statements ===&lt;br /&gt;
&lt;br /&gt;
The list below briefly presents the features making possible the creation of block statements in your script. Listed first are the more obvious ones, followed by those that are somewhat more esoteric and require a little digging inside the Avisynth documentation and experimenting with test cases to discover them.&lt;br /&gt;
&lt;br /&gt;
* globals (in particular, variables preceded by the &amp;quot;global&amp;quot; keyword) allow the communication of information between code blocks executing in different context.&lt;br /&gt;
 &lt;br /&gt;
* The conditional operator (condition ? expr_if_true : expr_if_false) can contain an arbitrary number of nested expressions, if grouped by parentheses.&lt;br /&gt;
 &lt;br /&gt;
* Strings can contain double quote (&amp;quot;) characters inside them if they are surrounded by three-double-quotes (&amp;quot;&amp;quot;&amp;quot;).&lt;br /&gt;
*: Thus, the following strings are valid in Avisynth script (note that the 2nd and 3rd ones could be lines in a script):&lt;br /&gt;
** &amp;quot;&amp;quot;&amp;quot;this is a string with &amp;quot; inside it&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
** &amp;quot;&amp;quot;&amp;quot;var = &amp;quot;a string value&amp;quot; &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
** &amp;quot;&amp;quot;&amp;quot;var = &amp;quot;a string value&amp;quot; # this is a comment&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
* There is a script function, [[Internal_functions/Control_functions|Eval()]], that allows the evaluation of strings containing arbitrary script expressions.&lt;br /&gt;
*: Thus, every expression that you can write in a script can be, if stored in a string, passed to Eval. &lt;br /&gt;
*: Eval returns the result of the evaluated expression, ie ''anything'' that can be constructed by such an expression (a clip, a number, a bool, a string).&lt;br /&gt;
*: The evaluation of the string is done in the same context as the call to Eval. Thus, if Eval is called at the script level, the expression is assumed to reference script-level variables or / and globals (globals are allowed everywhere). But if Eval is called inside a user-defined function then the expression is assumed to reference variables local to the function (ie arguments and any locally declared variable)&lt;br /&gt;
 &lt;br /&gt;
* There is a script function, [[Import]](), that allows the evaluation of arbitrary Avisynth scripts.&lt;br /&gt;
*: Thus, any script written in Avisynth script language can be evaluated by Import. Import returns the return value of the script. &lt;br /&gt;
*: Despite the common misbelief that this can only be a clip, it can actually be ''any'' type of variable (a clip, a number, a bool, a string).&lt;br /&gt;
*: Like Eval, the evaluation of the script is done in the same context as the call to Import.&lt;br /&gt;
*: Hence, as a side-effect of the script evaluation any functions and variables declared inside the imported script are accessible from the caller script, from the point of the Import call and afterwards.&lt;br /&gt;
&lt;br /&gt;
* Recursion (ie calling a function from inside that function) can be used for traversing elements of a collection. &lt;br /&gt;
*: Thus, for..next, do..while, do..until loops can be constructed by using recursion.&lt;br /&gt;
&lt;br /&gt;
* Multiline strings, ie strings that contain newlines (the CR/LF pair) inside them, are allowed by the script language.&lt;br /&gt;
&lt;br /&gt;
* Multiline strings are parsed by [[Internal_functions/Control_functions|Eval()]] as if they were scripts.&lt;br /&gt;
*: Thus, each line of a multiline string will be evaluated as if it was a line in a script. &lt;br /&gt;
*: Also, return statements inside the string are allowed (the value of their expression will be the return value of Eval), as well as comments, function calls and in general every feature of the script language.&lt;br /&gt;
&lt;br /&gt;
Consider the following '''Example 3''', of a (useless) script that returns some black frames followed by some white frames:&lt;br /&gt;
&lt;br /&gt;
'''Example 3'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 c = BlankClip().Trim(0,23)&lt;br /&gt;
 d = BlankClip(color=$ffffff).Trim(0,23)&lt;br /&gt;
 b = true&lt;br /&gt;
 dummy = b ? Eval(&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
	 k = c       # here comments are allowed!&lt;br /&gt;
	 l = d&lt;br /&gt;
	 return k    # this will be stored in dummy&lt;br /&gt;
	 &amp;quot;&amp;quot;&amp;quot;) : Eval(&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
	 k = d&lt;br /&gt;
	 l = c&lt;br /&gt;
	 return k    # this will be stored in dummy&lt;br /&gt;
	 &amp;quot;&amp;quot;&amp;quot;)&lt;br /&gt;
 # variables declared inside a multiline string&lt;br /&gt;
 # are available to the script after calling Eval&lt;br /&gt;
 return k + l&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Variables ''k'', ''l'' are not declared anywhere before the evaluation of the if..else block. However, since Eval evaluates the string at the script-level context, it is as if the statements inside the string were written at the script level. Therefore, after Eval() they are available to the script. A few other interesting things to note are the following:&lt;br /&gt;
&lt;br /&gt;
* The return statement at the end of the selected (by the value of ''b'') string for evaluation is the value that will be returned to the ''dummy'' variable.&lt;br /&gt;
&lt;br /&gt;
* Contrary to the case of line continuation by backslashes, a multiline string allows comments everywhere that they would be allowed in a script.&lt;br /&gt;
&lt;br /&gt;
== Implementation Guide ==&lt;br /&gt;
&lt;br /&gt;
The features above can be used to construct block statements in various ways. The most common implementation cases are presented in this section, grouped by block statement type.&lt;br /&gt;
&lt;br /&gt;
=== The if..else block statement ===&lt;br /&gt;
&lt;br /&gt;
==== Using Eval() and three-double-quotes quoted strings ==== &lt;br /&gt;
&lt;br /&gt;
This is by far the more flexible implementation, since the flow of text approaches most the &amp;quot;natural&amp;quot; (ie the commonly used in other languages) way of branching code execution. &lt;br /&gt;
&lt;br /&gt;
Using the rather common case illustrated by '''Example 1''', the solution would be (again items in square brackets are optional):&lt;br /&gt;
&lt;br /&gt;
'''Example 4'''&lt;br /&gt;
&lt;br /&gt;
 [result = ] cond ? Eval(&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
     filter1_1&lt;br /&gt;
     filter1_2&lt;br /&gt;
     ...&lt;br /&gt;
     filter1_n&lt;br /&gt;
   [ return {result of last filter} ]&lt;br /&gt;
     &amp;quot;&amp;quot;&amp;quot;) : Eval(&amp;quot;&amp;quot;&amp;quot; &lt;br /&gt;
     filter2_1&lt;br /&gt;
     filter2_2&lt;br /&gt;
     ...&lt;br /&gt;
     filter2_n&lt;br /&gt;
   [ return {result of last filter} ]&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
In short, you write the code blocks as if Avisynth script would support block statements and then enclose the blocks in three-double-quotes to make them multiline strings, wrap a call to [[Internal_functions/Control_functions|Eval]] around each string and finally assemble Eval calls into a conditional operator statement.&lt;br /&gt;
&lt;br /&gt;
The return statements at the end of each block are needed only if you want to assign a useful value to the ''result'' variable. If you simply want to execute the statements without returning a result, then you can omit the ''return'' statement at the end of each block. &lt;br /&gt;
&lt;br /&gt;
One important thing to note is that the implicit setting of ''last'' continues to work as normal inside the Eval block. If the result of Eval is assigned to a variable, ''last'' will not be updated for the final expression in the block (with or without ''return''), but it will be (where appropriate) for other statements in the block.&lt;br /&gt;
&lt;br /&gt;
If the block statement produces a result you intend to use, it is clearer to enter a ''return {result}'' line as the last line of each block, but the keyword ''return'' is not strictly necessary.&lt;br /&gt;
&lt;br /&gt;
The following real-case examples illustrate the above:&lt;br /&gt;
&lt;br /&gt;
'''Example 5'''&lt;br /&gt;
In this example, all results are assigned to script variables, so ''last'' is unchanged.&lt;br /&gt;
&lt;br /&gt;
 c = [[AviSource]](...)&lt;br /&gt;
 ...&lt;br /&gt;
 cond = {expr}&lt;br /&gt;
 ...&lt;br /&gt;
 cond ? Eval(&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
     text = &amp;quot;single double quotes are allowed inside three-double-quotes&amp;quot;&lt;br /&gt;
     pos = FindStr(text, &amp;quot;llo&amp;quot;)   # comments also&lt;br /&gt;
     d = c.[[Subtitle]](LeftStr(text, pos - 1))&lt;br /&gt;
 &amp;quot;&amp;quot;&amp;quot;) : Eval(&amp;quot;&amp;quot;&amp;quot; &lt;br /&gt;
     text = &amp;quot;thus by using three-double-quotes you can write expressions like you do in a script&amp;quot;&lt;br /&gt;
     pos = FindStr(text, &amp;quot;tes&amp;quot;)&lt;br /&gt;
     d = c.SubTitle(MidStr(text, pos + StrLen(&amp;quot;tes&amp;quot;)))&lt;br /&gt;
 &amp;quot;&amp;quot;&amp;quot;)&lt;br /&gt;
 return d&lt;br /&gt;
&lt;br /&gt;
'''Example 6'''&lt;br /&gt;
This example assigns a different clip to d depending on the [[Clip_properties|Framecount]] of a source clip.&lt;br /&gt;
&lt;br /&gt;
 a = AviSource(...)&lt;br /&gt;
 c = [[BlankClip]]().SubTitle(&amp;quot;a test case for an if..else block statement&amp;quot;)&lt;br /&gt;
 d = a.Framecount &amp;gt;= c.Framecount ? Eval(&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
     a = a.[[BilinearResize]](c.Width, c.Height)&lt;br /&gt;
     c = c.Tweak(hue=120)&lt;br /&gt;
     return [[Overlay]](a, c, opacity=0.5)&lt;br /&gt;
 &amp;quot;&amp;quot;&amp;quot;) : Eval(&amp;quot;&amp;quot;&amp;quot; &lt;br /&gt;
     c = c.BilinearResize(a.Width, a.Height)&lt;br /&gt;
     a = a.[[Tweak]](hue=120)&lt;br /&gt;
     return Overlay(c, a, opacity=0.5)&lt;br /&gt;
 &amp;quot;&amp;quot;&amp;quot;)&lt;br /&gt;
 return d&lt;br /&gt;
&lt;br /&gt;
'''Example 7'''&lt;br /&gt;
This example is a recode of Example 6 using implicit assignment to the ''last'' special variable. Since the result of the entire Eval() is not assigned to another variable, the implicit assignments to ''last'' on each line of the string (including the ''last line'' of the string) are preserved and thus the desired result is obtained.&lt;br /&gt;
&lt;br /&gt;
 c = BlankClip().SubTitle(&amp;quot;a test case for an if..else block statement&amp;quot;)&lt;br /&gt;
 AviSource(...)&lt;br /&gt;
 last.Framecount &amp;gt;= c.Framecount ? Eval(&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
     BilinearResize(c.Width, c.Height)&lt;br /&gt;
     c = c.Tweak(hue=120)&lt;br /&gt;
     Overlay(last, c, opacity=0.5)&lt;br /&gt;
 &amp;quot;&amp;quot;&amp;quot;) : Eval(&amp;quot;&amp;quot;&amp;quot; &lt;br /&gt;
     c = c.BilinearResize(last.Width, last.Height)&lt;br /&gt;
     Tweak(hue=120)&lt;br /&gt;
     Overlay(c, last, opacity=0.5)&lt;br /&gt;
 &amp;quot;&amp;quot;&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
The only disadvantage of the Eval approach is that coding errors inside the string blocks are masked by the [[Internal_functions/Control_functions|Eval()]] call, since the parser actually parses a '''single line''' of code:&lt;br /&gt;
&lt;br /&gt;
 [result = ] cond ? Eval(&amp;quot;&amp;quot;&amp;quot;block 1&amp;quot;&amp;quot;&amp;quot;) : Eval(&amp;quot;&amp;quot;&amp;quot;block 2&amp;quot;&amp;quot;&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Thus, any error(s) inside the blocks will be reported as a single error happening on the above line. You will not be pointed to the exact line of error as in normal script flow. Therefore, you will have to figure out where exactly the error occured, which can be a great debugging pain, especially if you write big blocks.&lt;br /&gt;
&lt;br /&gt;
==== Using separate scripts as blocks and the Import() function ====&lt;br /&gt;
&lt;br /&gt;
Using '''Example 1''' as above, the solution would be (again items in square brackets are optional):&lt;br /&gt;
&lt;br /&gt;
'''Example 8'''&lt;br /&gt;
Code of script file ''block1.avs'':&lt;br /&gt;
&lt;br /&gt;
 filter1_1&lt;br /&gt;
 filter1_2&lt;br /&gt;
 ...&lt;br /&gt;
 filter1_n&lt;br /&gt;
&lt;br /&gt;
Code of script file ''block2.avs'':&lt;br /&gt;
&lt;br /&gt;
 filter2_1&lt;br /&gt;
 filter2_2&lt;br /&gt;
 ...&lt;br /&gt;
 filter2_n&lt;br /&gt;
&lt;br /&gt;
Code of main script where the conditional branch is desired:&lt;br /&gt;
&lt;br /&gt;
 ...&lt;br /&gt;
 [result = ]cond ? Import(&amp;quot;block1.avs&amp;quot;) : Import(&amp;quot;block2.avs&amp;quot;)&lt;br /&gt;
 ...&lt;br /&gt;
&lt;br /&gt;
In short, you create separate scripts for each block and then conditionally import them at the main script. &lt;br /&gt;
&lt;br /&gt;
If you need to pass [[Script_variables|variables]] as &amp;quot;parameters&amp;quot; to the blocks, declare them in your main script and just reference them into the block scripts. The following example demonstrates this:&lt;br /&gt;
&lt;br /&gt;
'''Example 9'''&lt;br /&gt;
Code of script file ''block1.avs'':&lt;br /&gt;
&lt;br /&gt;
 filter1_1(..., param1, ...)&lt;br /&gt;
 filter1_2(..., param2, ...)&lt;br /&gt;
 ...&lt;br /&gt;
 filter1_n(..., param3, ...)&lt;br /&gt;
&lt;br /&gt;
Code of script file ''block2.avs'':&lt;br /&gt;
&lt;br /&gt;
 filter2_1(..., param1, ...)&lt;br /&gt;
 filter2_2(..., param2, ...)&lt;br /&gt;
 ...&lt;br /&gt;
 filter2_n(..., param3, ...)&lt;br /&gt;
&lt;br /&gt;
Code of main script where the conditional branch is desired:&lt;br /&gt;
&lt;br /&gt;
 # variables must be defined *before* importing the block script&lt;br /&gt;
 param1 = ...&lt;br /&gt;
 param2 = ...&lt;br /&gt;
 param3 = ...&lt;br /&gt;
 ...&lt;br /&gt;
 [result = ]cond ? Import(&amp;quot;block1.avs&amp;quot;) : Import(&amp;quot;block2.avs&amp;quot;)&lt;br /&gt;
 ...&lt;br /&gt;
&lt;br /&gt;
Using [[Import]]() instead of [[Internal_functions/Control_functions|Eval()]] and three-double-quoted multiline strings has some disadvantages: &lt;br /&gt;
&lt;br /&gt;
* There is an administration overhead because instead of one file ''2k + 1'' files have to be maintained (''k'' = the number of conditional branches in your script).&lt;br /&gt;
* The code has less clarity, in the sense that it does not visually appears as a block statement, neither the communication of parameters is apparent by inspection of the main script.&lt;br /&gt;
&lt;br /&gt;
On the other hand:&lt;br /&gt;
&lt;br /&gt;
* Debugging is not an issue; every error will be reported with accurate line information.&lt;br /&gt;
* You can reuse scripts that you frequently use and build more complex ones by simply importing ready-made components.&lt;br /&gt;
* For large-scale operations where few parameters have to be communicated it is usually a better approach.&lt;br /&gt;
&lt;br /&gt;
One useful general purpose application of this implementation is to prototype, test and debug a block conditional branch and then recode it (by adding the Eval() and three-double-quotes wrapper code and removing the [[Script_variables|global]] keyword before the parameter's declarations) so that a single script using multiline strings as blocks is created. This workaround compensates for the main disadvantage of the Eval() and three-double-quotes implementation.&lt;br /&gt;
&lt;br /&gt;
==== Using functions (one function for each block) ==== &lt;br /&gt;
&lt;br /&gt;
This is the most &amp;quot;loyal&amp;quot; to the Avisynth script's [[AviSynth_Syntax|syntax]] approach. Using '''Example 1''' as above, the solution would be (again items in square brackets are optional):&lt;br /&gt;
&lt;br /&gt;
'''Example 10'''&lt;br /&gt;
&lt;br /&gt;
 Function block_if_1()&lt;br /&gt;
 {&lt;br /&gt;
     filter1_1&lt;br /&gt;
     filter1_2&lt;br /&gt;
     ...&lt;br /&gt;
     filter1_n&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 Function block_else_1()&lt;br /&gt;
 {&lt;br /&gt;
     filter2_1&lt;br /&gt;
     filter2_2&lt;br /&gt;
     ...&lt;br /&gt;
     filter2_n&lt;br /&gt;
 }&lt;br /&gt;
 ...&lt;br /&gt;
 [result = ]cond ? block_if_1() : block_else_1()&lt;br /&gt;
 ...&lt;br /&gt;
&lt;br /&gt;
In short, you create separate functions for each block and then conditionally call them at the branch point. &lt;br /&gt;
&lt;br /&gt;
If you need to pass variables as &amp;quot;parameters&amp;quot; to the blocks, either declare them ''global'' in your main script and just reference them into the functions or - better - use argument lists at the functions. The following example demonstrates this:&lt;br /&gt;
&lt;br /&gt;
'''Example 11'''&lt;br /&gt;
&lt;br /&gt;
 Function block_if_1(arg1, arg2, arg3, ...)&lt;br /&gt;
 {&lt;br /&gt;
     filter1_1(..., arg1, ...)&lt;br /&gt;
     filter1_2(..., arg2, ...)&lt;br /&gt;
     ...&lt;br /&gt;
     filter1_n(..., arg3, ...)&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 Function block_else_1(arg1, arg2, arg3, ...)&lt;br /&gt;
 {&lt;br /&gt;
     filter2_1(..., arg1, ...)&lt;br /&gt;
     filter2_2(..., arg2, ...)&lt;br /&gt;
     ...&lt;br /&gt;
     filter2_n(..., arg3, ...)&lt;br /&gt;
 }&lt;br /&gt;
 ...&lt;br /&gt;
 [result = ]cond \&lt;br /&gt;
     ? block_if_1(arg1, arg2, arg3, ...) \&lt;br /&gt;
     : block_else_1(arg1, arg2, arg3, ...)&lt;br /&gt;
 ...&lt;br /&gt;
&lt;br /&gt;
Compared to the other two implementations this one has the following disadvantages:&lt;br /&gt;
&lt;br /&gt;
* There is an extra overhead due to the need for supplying function headers and (typically) argument lists.&lt;br /&gt;
* It tends to &amp;quot;pollute&amp;quot; the global namespace, thus having the potential of strange errors due to name conflicts; use a clear naming scheme, as the suggested above. &lt;br /&gt;
&lt;br /&gt;
On the other hand:&lt;br /&gt;
&lt;br /&gt;
* It is '''portable'''; it does not depend on any type of hack or specific behavior to work. It is thus guaranteed to continue working in the long term.&lt;br /&gt;
* It does not raise any special debuging difficulties.&lt;br /&gt;
* It has coding clarity.&lt;br /&gt;
&lt;br /&gt;
=== The if..elif..else block statement === &lt;br /&gt;
&lt;br /&gt;
By nesting If..Else block expressions inside the conditional operator, you can create entire if..elseif...else conditional constructs of any level desired to accomodate more complex needs. &lt;br /&gt;
&lt;br /&gt;
A generic example for each if..else implementation presented above is following. Of course, any combination of the three above pure cases is possible.&lt;br /&gt;
&lt;br /&gt;
==== Using Eval() and three-double-quotes quoted strings ==== &lt;br /&gt;
&lt;br /&gt;
The solution would be (again items in square brackets are optional):&lt;br /&gt;
&lt;br /&gt;
'''Example 12'''&lt;br /&gt;
&lt;br /&gt;
 [result = \]&lt;br /&gt;
     cond_1 ? Eval(&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
         statement 1_1&lt;br /&gt;
         ...&lt;br /&gt;
         statement 1_n&lt;br /&gt;
     &amp;quot;&amp;quot;&amp;quot;) : [(] \ &lt;br /&gt;
     cond_2 ? Eval(&amp;quot;&amp;quot;&amp;quot; # inner a?b:c enclosed in parentheses for clarity (optional)&lt;br /&gt;
         statement 2_1&lt;br /&gt;
         ...           # since backslash line continuation is used between Eval blocks&lt;br /&gt;
         statement 2_n # place comments only inside the strings&lt;br /&gt;
     &amp;quot;&amp;quot;&amp;quot;) : [(] \ &lt;br /&gt;
     ...&lt;br /&gt;
     cond_n ? Eval(&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
         statement n_1&lt;br /&gt;
         ...&lt;br /&gt;
         statement n_n&lt;br /&gt;
     &amp;quot;&amp;quot;&amp;quot;) \&lt;br /&gt;
     : Eval(&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
         statement n+1_1&lt;br /&gt;
         ...&lt;br /&gt;
         statement n+1_n&lt;br /&gt;
     &amp;quot;&amp;quot;&amp;quot;)[...))]  # 1 closing parenthesis for Eval() + n-1 to balance the opening ones (if used)&lt;br /&gt;
&lt;br /&gt;
==== Using separate scripts as blocks and the Import() function ====&lt;br /&gt;
&lt;br /&gt;
The solution would be (again items in square brackets are optional):&lt;br /&gt;
&lt;br /&gt;
'''Example 13'''&lt;br /&gt;
&lt;br /&gt;
 # here no comments are allowed; every line but the last must end with a \&lt;br /&gt;
 [result = \]&lt;br /&gt;
     cond_1 ? \&lt;br /&gt;
         Import(&amp;quot;block1.avs&amp;quot;) : [(] \&lt;br /&gt;
     cond_2 ? \&lt;br /&gt;
         Import(&amp;quot;block2.avs&amp;quot;) : [(] \&lt;br /&gt;
     ...&lt;br /&gt;
     cond_n ? \&lt;br /&gt;
         Import(&amp;quot;blockn.avs&amp;quot;) \&lt;br /&gt;
     : \&lt;br /&gt;
         Import(&amp;quot;block-else.avs&amp;quot;) \&lt;br /&gt;
     [)...))]  # n-1 closing parentheses to balance the opening ones (if used)&lt;br /&gt;
&lt;br /&gt;
==== Using functions (one function for each block) ==== &lt;br /&gt;
&lt;br /&gt;
The solution would be (again items in square brackets are optional):&lt;br /&gt;
&lt;br /&gt;
'''Example 14'''&lt;br /&gt;
&lt;br /&gt;
 # here no comments are allowed; every line but the last must end with a \&lt;br /&gt;
 [result = \]&lt;br /&gt;
     cond_1 ? \&lt;br /&gt;
         function_block_1({arguments}) : [(] \&lt;br /&gt;
     cond_2 ? \&lt;br /&gt;
         function_block_2({arguments}) : [(] \&lt;br /&gt;
     ...&lt;br /&gt;
     cond_n ? \&lt;br /&gt;
         function_block_n({arguments}) \&lt;br /&gt;
     : \&lt;br /&gt;
         function_block_else({arguments}) \&lt;br /&gt;
     [)...))]  # n-1 closing parentheses to balance the opening ones (if used)&lt;br /&gt;
&lt;br /&gt;
=== The for..next block statement ===&lt;br /&gt;
&lt;br /&gt;
The problem here is to implement the &amp;lt;tt&amp;gt;for..next&amp;lt;/tt&amp;gt; loop in a way that allows accessing variables in the local scope, so that changes made in local scope variables inside the loop can be accessible by the caller when it is finished. This is the way that the &amp;lt;tt&amp;gt;for..next&amp;lt;/tt&amp;gt; loop works in most programming languages that provide it. In addition, a means for getting out of the loop before is finished (ie breaking out of the loop) should be available.&lt;br /&gt;
&lt;br /&gt;
There is of course the alternative to implement the &amp;lt;tt&amp;gt;for..next&amp;lt;/tt&amp;gt; loop in a way that does not allow access to local variables. This is easier in AviSynth, since then it can be implemented by a function; but it is also less useful. However in many cases it would be appropriate to use such a construct and thus it will be presented here.&lt;br /&gt;
&lt;br /&gt;
==== For..Next loop with access to variables in local scope ====&lt;br /&gt;
&lt;br /&gt;
# Use a &amp;lt;tt&amp;gt;ForNext(start, end, step, blocktext)&amp;lt;/tt&amp;gt; function to create a multiline string (a script) that will unroll the loop in a series of statements and then &lt;br /&gt;
# use Eval() to execute the script in the current scope. &lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;blocktext&amp;lt;/tt&amp;gt; is a script text, typically a multiline string in triple double quotes, that contains the instructions to be executed in each loop, along with special variables (say ${i} for the loop counter) that are textually replaced by the &amp;lt;tt&amp;gt;ForNext&amp;lt;/tt&amp;gt; function with the current value(s) in each loop. The [http://avslib.sourceforge.net/functions/s/strreplace.html StrReplace()] function is particularly suited for the replacement task.&lt;br /&gt;
&lt;br /&gt;
A little tweak is needed in order to implement the &amp;lt;tt&amp;gt;break&amp;lt;/tt&amp;gt; statement; the unrolled string must be constructed in such a way that when the break flag is set the rest of the code is skipped.&lt;br /&gt;
&lt;br /&gt;
The following proof-of-concept example demonstrates the procedure:&lt;br /&gt;
&lt;br /&gt;
 a = [[AviSource]](&amp;quot;c:\some.avi&amp;quot;)&lt;br /&gt;
 cnt = 12&lt;br /&gt;
 b = a.[[Trim]](0,-4) &lt;br /&gt;
 cond = false&lt;br /&gt;
 &lt;br /&gt;
 # here we would like to do the following&lt;br /&gt;
 # for (i = 0; i &amp;lt; 6; i++) {&lt;br /&gt;
 #    b = b + a.Trim(i*cnt, -4)&lt;br /&gt;
 #    cond = b.Framecount() &amp;gt; 20 ? true : false&lt;br /&gt;
 #    if (cond)&lt;br /&gt;
 #        break&lt;br /&gt;
 # }&lt;br /&gt;
 &lt;br /&gt;
 return b&lt;br /&gt;
&lt;br /&gt;
In order to make this happen in AviSynth, our script with &amp;lt;tt&amp;gt;ForNext&amp;lt;/tt&amp;gt; would look like that:&lt;br /&gt;
&lt;br /&gt;
 a = [[AviSource]](&amp;quot;c:\some.avi&amp;quot;)&lt;br /&gt;
 cnt = 12&lt;br /&gt;
 b = a.[[Trim]](0,-4) &lt;br /&gt;
 block = ForNext(0, 5, 1, &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
     b = b + a.Trim(${i}*cnt, -4)&lt;br /&gt;
     cond = b.Framecount() &amp;gt; 20 ? true : false&lt;br /&gt;
     ${break(cond)}&lt;br /&gt;
     &amp;quot;&amp;quot;&amp;quot;)&lt;br /&gt;
 void = Eval(block)&lt;br /&gt;
 return b&lt;br /&gt;
&lt;br /&gt;
or more succinctly:&lt;br /&gt;
&lt;br /&gt;
 a = [[AviSource]](&amp;quot;c:\some.avi&amp;quot;)&lt;br /&gt;
 cnt = 12&lt;br /&gt;
 b = a.[[Trim]](0,-4) &lt;br /&gt;
 void = Eval(ForNext(0, 5, 1, &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
     b = b + a.Trim(${i}*cnt, -4)&lt;br /&gt;
     cond = b.Framecount() &amp;gt; 20 ? true : false&lt;br /&gt;
     ${break(cond)}&lt;br /&gt;
     &amp;quot;&amp;quot;&amp;quot;))&lt;br /&gt;
 return b&lt;br /&gt;
&lt;br /&gt;
and the output of ForNext with the above arguments should be something like this (the only problem is that string literals cannot be typed inside the block text):&lt;br /&gt;
&lt;br /&gt;
 &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
 __break = false&lt;br /&gt;
 dummy = __break ? NOP : Eval(&amp;quot;&lt;br /&gt;
     b = b + a.Trim(0*cnt, -4)&lt;br /&gt;
     cond = b.Framecount() &amp;gt; 20 ? true : false&lt;br /&gt;
     __break = cond ? true : false&lt;br /&gt;
 &amp;quot;)&lt;br /&gt;
 dummy = __break ? NOP : Eval(&amp;quot;&lt;br /&gt;
     b = b + a.Trim(1*cnt, -4)&lt;br /&gt;
     cond = b.Framecount() &amp;gt; 20 ? true : false&lt;br /&gt;
     __break = cond ? true : false&lt;br /&gt;
 &amp;quot;)&lt;br /&gt;
 dummy = __break ? NOP : Eval(&amp;quot;&lt;br /&gt;
     b = b + a.Trim(2*cnt, -4)&lt;br /&gt;
     cond = b.Framecount() &amp;gt; 20 ? true : false&lt;br /&gt;
     __break = cond ? true : false&lt;br /&gt;
 &amp;quot;)&lt;br /&gt;
 dummy = __break ? NOP : Eval(&amp;quot;&lt;br /&gt;
     b = b + a.Trim(3*cnt, -4)&lt;br /&gt;
     cond = b.Framecount() &amp;gt; 20 ? true : false&lt;br /&gt;
     __break = cond ? true : false&lt;br /&gt;
 &amp;quot;)&lt;br /&gt;
 dummy = __break ? NOP : Eval(&amp;quot;&lt;br /&gt;
     b = b + a.Trim(4*cnt, -4)&lt;br /&gt;
     cond = b.Framecount() &amp;gt; 20 ? true : false&lt;br /&gt;
     __break = cond ? true : false&lt;br /&gt;
 &amp;quot;)&lt;br /&gt;
 dummy = __break ? NOP : Eval(&amp;quot;&lt;br /&gt;
     b = b + a.Trim(5*cnt, -4)&lt;br /&gt;
     cond = b.Framecount() &amp;gt; 20 ? true : false&lt;br /&gt;
     __break = cond ? true : false&lt;br /&gt;
 &amp;quot;)&lt;br /&gt;
 &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
&lt;br /&gt;
TO BE CONTINUED...&lt;br /&gt;
&lt;br /&gt;
==== For..Next loop without access to variables in local scope ====&lt;br /&gt;
&lt;br /&gt;
If we don't care for accessing variables in the local scope, then the implementation is straightforward: &lt;br /&gt;
&lt;br /&gt;
# Create an [[Arrays|AVSLib array]] with the appropriate loop values.&lt;br /&gt;
# Define needed globals (for example a bool flag to return immediately from the block if true).&lt;br /&gt;
# Pack the block's code inside a function.&lt;br /&gt;
# Use an [http://avslib.sourceforge.net/tutorials/operators.html array operator] to execute the block for every loop value.&lt;br /&gt;
&lt;br /&gt;
TO BE CONTINUED...&lt;br /&gt;
&lt;br /&gt;
=== The do..while and do..until block statements ===&lt;br /&gt;
&lt;br /&gt;
TODO...&lt;br /&gt;
&lt;br /&gt;
== Deciding which implementation to use ==&lt;br /&gt;
&lt;br /&gt;
To be frank, there is no clear-cut answer to this question; it depends on the purpose that the script will serve, your coding abilities and habits, whether there are ready-made components available and what type are they (scripts, function libraries, etc.) and similar factors.&lt;br /&gt;
&lt;br /&gt;
Thus, only some generic guidelines will be presented here, grouped on the type of block statement&lt;br /&gt;
&lt;br /&gt;
=== The if..else and if..elif..else block statements ===&lt;br /&gt;
&lt;br /&gt;
* For short (up to say 10 lines) blocks, using Eval() and three-double-quotes quoted strings is generally the best solution; it is fast to code and presents a &amp;quot;natural&amp;quot; text flow to the reader (thus it is easy to comprehend).&lt;br /&gt;
&lt;br /&gt;
* For long blocks, using any of the other two implementations is generally better because it is easier to debug. &lt;br /&gt;
&lt;br /&gt;
* If the blocks pre-exist as independent scripts, using [[Import]]() is, obviously, preferred.&lt;br /&gt;
&lt;br /&gt;
* If building a function library, usually an implementation with functions will be easier to maintain and debug. However using [[Internal_functions/Control_functions|Eval]]() for small blocks is still an option to consider, to minimise the risk of namespace clashing with user's own functions.&lt;br /&gt;
&lt;br /&gt;
=== The for..next block statement ===&lt;br /&gt;
&lt;br /&gt;
TODO...&lt;br /&gt;
&lt;br /&gt;
=== The do..while and do..until block statements ===&lt;br /&gt;
&lt;br /&gt;
TODO...&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
[1] http://www.avisynth.org/stickboy/ternary_eval.html&lt;br /&gt;
&lt;br /&gt;
[2] http://forum.doom9.org/showthread.php?t=102929&lt;br /&gt;
&lt;br /&gt;
[3] http://forum.doom9.org/showthread.php?p=732882#post732882&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
Back to [[Scripting_reference|scripting reference]].&lt;br /&gt;
[[Category:AviSynth_Syntax]]&lt;br /&gt;
[[Category:Scripting_Reference]]&lt;/div&gt;</summary>
		<author><name>Unreal666</name></author>	</entry>

	</feed>