If I include the following function definition in a Q script
test:{
$[x > 0; [“greater”];[“less”]]
}
I get this when I attempt to load the script:
k){0N! x y}
'{
@
“q”
“test:{{\n $[x > 0;["greater"];["less"]]”
But if I just give the interpreter the conditional expression
$[x > 0; [“greater”];[“less”]]
it gives the correct result, assuming x was previously given a value.
What am I doing wrong in the script? In case it’s not obvious, I’m a Q newbie.
put a single space in front of the closing }there’s a scripting rules wiki page on code.kx.com somewhere, i can’tfind it now…ps, you don’t need the inside the $ conditionOn Thu, Dec 11, 2014 at 9:41 PM, Stuart Smith <stuart.smith3> wrote:>> If I include the following function definition in a Q script>> test:{> $[x > 0; [“greater”];[“less”]]> }>>> I get this when I attempt to load the script:>> k){0N! x y}> '{> @> “q”> “test:{{\n $[x > 0;["greater"];["less"]]”>>> But if I just give the interpreter the conditional expression>> $[x > 0; [“greater”];[“less”]]>> it gives the correct result, assuming x was previously given a value.>>> What am I doing wrong in the script? In case it’s not obvious, I’m a Q> newbie.>> –>
Submitted via Google Groups</stuart.smith3>
Thanks, it works! I’m sure that the answers to most of my newbie Q questions will be answered in Jeff Borror’s helpful 450+ page “Q For Mortals.” The problem is where. If you ever remember where the scripting rules are, please let me know. It appears they would save me and other beginners a lot of unnecessarily wasted time and trouble.
-SS
===
I spent a whole day once trying to figure out how to get my function to work until my boss came over and said just add a space there. I don’t think I have seen this rule written anywhere on kx website. It’s just something you learn on the job from the more experienced devs :)
Also, if I were you, I would reword the function as:
test:{$[x>0;“greater”;“less”]};
q has first three arguments already built in as x,y,z.
Ah, was in good old Q4M :)
http://code.kx.com/wiki/JB:QforMortals/execution\_control#Scripts
“Multi-line expressions are permitted in a script but they have a
special form. The first line must be out-dented, meaning that it
begins at the left of the line with no initial white space. Any
continuation lines must be indented, meaning that there is at least
one white space character at the beginning of the line. Empty lines
between expressions are permitted.”