Welcome to Doom9's Forum, THE in-place to be for everyone interested in DVD conversion. Before you start posting please read the forum rules. By posting to this forum you agree to abide by the rules. |
26th March 2013, 09:13 | #1 | Link |
Registered User
Join Date: Aug 2008
Location: Isle of Man
Posts: 588
|
Multiple statements on a single line
This is probably old hat to the old-timers here, but I stumbled across something quite exciting. Avisynth does support multiple statements on a single line, in contradiction to Multiple Avisynth statements on a single line can only be achieved in the context of OOP notation or embedding filters as parameters of another function.
This seems to be supported for normal statements, by Eval() etc. and opens up a whole new world (for me at least ) of things like simple conditional block statements. This is with 2.6.0, I haven't tested with earlier versions. Here's an example of a trivial function returning multiple values Code:
#------------------------------------------- # ColourCoefficients 1.0.0 # Generates specified colour matrix coefficient values # # Useful when # # Doing YUV<->RGB colourspace calculations or conversions # Doing colour coefficient calculations or conversions # # ColourCoefficients(string "matrix") # # <matrix> Colour coefficient matrix which to generate coefficients for - "Rec601" / "Rec709" / "FCC", default "Rec601" # # Usage # # coeffs = ColourCoefficients("Rec709") # ColourCoefficients returns a multi-statement string with matrix coefficient value assignments # Eval(coeffs) # This defines Kr containing Red channel coefficient, Kg with Green channel coefficient and Kb for Blue channel # y = Kr*r + Kg*g + Kb*b # ... # # References # # http://avisynth.org/mediawiki/Color_conversions # # Version history # # 1.0.0 Francois Visagie # function ColourCoefficients(string "matrix") { matrix = Default(matrix, "Rec601") matrixnum = (matrix == "Rec601") ? 0 : \ (matrix == "Rec709") ? 1 : \ (matrix == "FCC" ) ? 2 : 3 Assert(matrixnum < 3, """'matrix' value """" + matrix + """" is invalid""") # Channel coefficients Rec.601 Rec.709 FCC Kr = Select(matrixnum, 0.299, 0.2125, 0.3 ) Kg = Select(matrixnum, 0.587, 0.7154, 0.59) Kb = Select(matrixnum, 0.114, 0.0721, 0.11) return("Kr=" + string(Kr) + " Kg=" + string(Kg) + " Kb=" + string(Kb)) } |
26th March 2013, 12:09 | #2 | Link |
Avisynth language lover
Join Date: Dec 2007
Location: Spain
Posts: 3,431
|
Yes, that's right.
Newline (unless accompanied by a '\') always terminates the current statement, but it is not strictly necessary. The Avisynth parser recognises it has reached the end of a statement when the next symbol is not a valid continuation of what it already has. So Code:
x = 1 y = 2 z = 3 Although it is conventional to use newlines to separate statements (and good practice for readability), the syntax is such that it is only strictly necessary if the following statement starts with a unary minus (or plus) operator - not very common, but can occur in cases like (silly example!): Code:
function f(int x, int y) { z = x + y - z # meaning return -z } |
26th March 2013, 18:32 | #4 | Link |
Excessively jovial fellow
Join Date: Jun 2004
Location: rude
Posts: 1,100
|
You could just have put newlines in the returned string, you know. There are also several better ways to "return" multiple values from a function. Using a callback function immediately comes to mind, but I'd say even global variables is cleaner than this solution.
|
27th March 2013, 08:36 | #5 | Link | |
Registered User
Join Date: Aug 2008
Location: Isle of Man
Posts: 588
|
Quote:
The only other syntax I found that works is Code:
return("Kr=" + string(Kr) + Chr(10) + "Kg=" + string(Kg) + Chr(10) + "Kb=" + string(Kb)) # or Chr(13) |
|
27th March 2013, 09:26 | #7 | Link | |
HeartlessS Usurer
Join Date: Dec 2009
Location: Over the rainbow
Posts: 10,980
|
I see nothing wrong with either space or Chr(10) string concatenation on a 'once in a while' usage, eg
clipwise return string, but not too good an idea on framewise or pixelwise usage, mainly due to fact that there is no dead string garbage collection and freeing of memory. The Gavino noted ' - z' thing would not come into it. EDIT: Quote:
__________________
I sometimes post sober. StainlessS@MediaFire ::: AND/OR ::: StainlessS@SendSpace "Some infinities are bigger than other infinities", but how many of them are infinitely bigger ??? Last edited by StainlessS; 27th March 2013 at 09:30. |
|
9th April 2013, 10:19 | #10 | Link |
Registered User
Join Date: Aug 2008
Location: Isle of Man
Posts: 588
|
For the record, it seems this is a bit of a moot point (as you guys probably knew all along ). As far as I can determine the only expressions accepted by the ternary operator are function calls, be it internal, plugin or script functions. It does not seem to accept assignment statements, unless wrapped in Eval(), which kind of defeats the purpose from this point of view .
|
9th April 2013, 11:56 | #11 | Link |
HeartlessS Usurer
Join Date: Dec 2009
Location: Over the rainbow
Posts: 10,980
|
Code:
x = (z == true) ? Q * 17 : Q * 21 # assignment (z == false) ? Do_somthing(q) : Do_something_else(q) # select one of two funcs (z == true) ? Do_anything(q) : Nop # conditional do something
__________________
I sometimes post sober. StainlessS@MediaFire ::: AND/OR ::: StainlessS@SendSpace "Some infinities are bigger than other infinities", but how many of them are infinitely bigger ??? Last edited by StainlessS; 9th April 2013 at 12:15. |
9th April 2013, 14:17 | #12 | Link | ||
Avisynth language lover
Join Date: Dec 2007
Location: Spain
Posts: 3,431
|
Quote:
Quote:
However, an assignment is not an expression, so can appear only at the start of a statement (statement form 1). |
||
23rd January 2014, 01:37 | #14 | Link | |
Registered User
Join Date: Sep 2005
Location: Vancouver
Posts: 600
|
Quote:
I would change the line myself but I don't want to make it wrong in a different way... |
|
|
|