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.

 Doom9's Forum Multiple statements on a single line
 User Name Remember Me? Password
 Register FAQ Calendar Search Today's Posts Mark Forums Read

 Thread Tools Search this Thread Display Modes
 26th March 2013, 09:13 #1  |  Link fvisagie 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") # # 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)) }``` To what extent has anyone else played with this, and what gotchas have been discovered so far?
 26th March 2013, 12:09 #2  |  Link Gavino Avisynth language lover   Join Date: Dec 2007 Location: Spain Posts: 3,412 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` is parsed as three separate (assignment) statements. 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 }``` Here '- z' must appear on a separate line, since the previous statement could validly be continued with a '-'. __________________ GScript and GRunT - complex Avisynth scripting made easier
 26th March 2013, 13:55 #3  |  Link fvisagie Registered User   Join Date: Aug 2008 Location: Isle of Man Posts: 588 Thanks for the explanation, Gavino.
 26th March 2013, 18:32 #4  |  Link TheFluff Excessively jovial fellow   Join Date: Jun 2004 Location: rude Posts: 1,098 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
fvisagie
Registered User

Join Date: Aug 2008
Location: Isle of Man
Posts: 588
Quote:
 Originally Posted by TheFluff You could just have put newlines in the returned string, you know.
I also thought so, but in this case I've found no way of writing the multi-line return string in a natural way that doesn't cause Eval() to fail. I've tried with literal "\n", variables by that value, triple quotes etc.

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)```
although that defeats the purpose even further, so to speak.

27th March 2013, 09:25   #6  |  Link
fvisagie
Registered User

Join Date: Aug 2008
Location: Isle of Man
Posts: 588
Quote:
 Originally Posted by TheFluff Using a callback function immediately comes to mind
How is this done in Avisynth (without using globals)?

27th March 2013, 09:26   #7  |  Link
StainlessS
HeartlessS Usurer

Join Date: Dec 2009
Location: Over the rainbow
Posts: 9,263
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:
 How is this done in Avisynth (without using globals)?
No Idea (In script).
__________________
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.

27th March 2013, 10:40   #8  |  Link
Gavino
Avisynth language lover

Join Date: Dec 2007
Location: Spain
Posts: 3,412
Quote:
 Originally Posted by fvisagie I've found no way of writing the multi-line return string in a natural way
You can use newlines directly inside a string literal, eg
Code:
```multiline = "first line
second line"```
__________________
GScript and GRunT - complex Avisynth scripting made easier

27th March 2013, 10:45   #9  |  Link
TheFluff
Excessively jovial fellow

Join Date: Jun 2004
Location: rude
Posts: 1,098
Quote:
 Originally Posted by fvisagie How is this done in Avisynth (without using globals)?
Code:
```function foo(int x, string callback_name) {
apply(callback_name, x)
}

function bar(int z) {
# do something with z
}

y = 5
foo(y, 'bar')```

9th April 2013, 10:19   #10  |  Link
fvisagie
Registered User

Join Date: Aug 2008
Location: Isle of Man
Posts: 588
Quote:
 Originally Posted by fvisagie things like simple conditional block statements
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 StainlessS HeartlessS Usurer     Join Date: Dec 2009 Location: Over the rainbow Posts: 9,263 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
Gavino
Avisynth language lover

Join Date: Dec 2007
Location: Spain
Posts: 3,412
Quote:
 Originally Posted by fvisagie 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...
From http://avisynth.org/mediawiki/Grammar:
Quote:
 All basic AviSynth scripting statements have one of these forms: 1. variable_name = expression 2. expression 3. return expression ... An expression can have one of these forms: 1. numeric_constant, string_constant or boolean_constant 2. variable_name or clip_property 3. Function(args) 4. expression.Function(args) 5. expression1 operator expression2 6. bool_expression ? expression1 : expression2
The expressions accepted by the ternary operator (ie in expression form 6) are any of the forms 1-6, so are not limited to function calls (forms 3 and 4).
However, an assignment is not an expression, so can appear only at the start of a statement (statement form 1).
__________________
GScript and GRunT - complex Avisynth scripting made easier

 9th April 2013, 14:46 #13  |  Link fvisagie Registered User   Join Date: Aug 2008 Location: Isle of Man Posts: 588 Thanks, guys. Now to read and apply documentation as attentively as you do...
23rd January 2014, 01:37   #14  |  Link
ChiDragon
Registered User

Join Date: Sep 2005
Location: Vancouver
Posts: 608
Quote:
 Originally Posted by fvisagie 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.
Could someone who understands this issue in-depth update the Wiki to be more accurate? I was recently led astray.

I would change the line myself but I don't want to make it wrong in a different way...

 Thread Tools Search this Thread Search this Thread: Advanced Search Display Modes Linear Mode

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home Announcements and Chat     General Discussion     News     Forum / Site Suggestions & Help General     Decrypting     Newbies     DVD2AVI / DGIndex     Audio encoding     Subtitles     Linux, Mac OS X, & Co Capturing and Editing Video     Avisynth Usage     Avisynth Development     VapourSynth     Capturing Video     DV     HDTV / DVB / TiVo     NLE - Non Linear Editing     VirtualDub, VDubMod & AviDemux     New and alternative a/v containers Video Encoding     (Auto) Gordian Knot     MPEG-4 ASP     MPEG-4 Encoder GUIs     MPEG-4 AVC / H.264     High Efficiency Video Coding (HEVC)     New and alternative video codecs     MPEG-2 Encoding     VP9 and AV1 (HD) DVD, Blu-ray & (S)VCD     One click suites for DVD backup and DVD creation     DVD & BD Rebuilder     (HD) DVD & Blu-ray authoring     Advanced authoring     IFO/VOB Editors     DVD burning Hardware & Software     Software players     Hardware players     PC Hard & Software Programming and Hacking     Development     Translations

All times are GMT +1. The time now is 15:39.

 Doom9.org - Archive - Top

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2021, vBulletin Solutions Inc.