Assign¶
Name a value; amend a named value
Simple assign¶
x:y
Where x
is a name and y
is a value, the value of y
is associated with the name x
.
q)a:42 / assign
q)a
42
q)a:3.14159 / amend
The Equal operator =
tests equality. It has nothing to do with naming or amending values.
There is no need to declare the type of a variable.
A variable acquires the type of the value assigned to it. (Known as dynamic typing.)
q)type a:til 5 / integer vector
7h
q)type a:3.14159 / float atom
-9h
Indexed assign¶
x[i]:y
Where
x
is the name of a list, dictionary or tablei
is a value that indexesx
(including a;
-separated multi-level index)y
is a scalar, or a list of the same shape asi
the value of y
is assigned to x
at indexes i
.
Indexed assignment to a simple list cannot change the type of x
.
If x
is a simple list (has type between 1 and 20) then (=). abs type each(x;y)
must be true. However, a general list that ends up having all items of the same atomic type after assignment will collapse into a simple list.
Where x
is a dictionary, assignment has upsert semantics.
q)s:1 2 3
q)s[1]:4
q)s
1 4 3
q)s[2]:5f
'type
[0] s[2]:5f
^
q)s:(1;2f;3)
q)s
1
2f
3
q)s[1]:4
q)s
1 4 3
q)s[1]:5f
'type
[0] s[1]:5f
^
q)m:(1 2;3 4)
q)m[1;1]:5
q)m
1 2
3 5
q)d:`tom`dick`harry!1 2 3
q)d[`dick`jane]:100 200
q)d
tom | 1
dick | 100
harry| 3
jane | 200
Assign through operator¶
x op:y op:[x;y]
x[i]op:y
Where
op
is a binary operator with infix syntaxx
orx[i]
is an assignable in the left domain ofop
(the rules of indexed assign apply forx[i]
)y
is a value in the right domain ofop
that conforms tox
(ori
)
the value of x
(or x[i]
) becomes x op y
(or x[i]op y
).
q)s:("the";"quick";"brown";"fox")
q)s[1 2],:("er";"ish")
q)s
"the"
"quicker"
"brownish"
"fox"
Extend Assign-through-operator to derived functions, keywords and lambdas.
q)s:("the";"quick";"brown";"fox")
q)@[s;1 2;,;("er";"ish")]
"the"
"quicker"
"brownish"
"fox"
Amend At is more general, and extends assignment-through-operator to derived functions, keywords and lambdas.
If x
is undefined, the identity element for op
is used as a default.
q)bar
'bar
[0] bar
^
q)bar+:1
q)bar
1
Some operators have significant differences between their base and assignment form, for example ,
.
Pattern match¶
See Pattern matching
Syntax¶
An expression with an assignment on the left returns no value to the console.
q)a:til 5
q)
The value of an assignment is the value assigned.
q)3+a:til 5
3 4 5 6 7
q)1+a[2]+:5
8
q)a
0 1 7 3 4
Amend, Amend At
Q for Mortals
§4.6.2 Simple q Amend