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
y
is a scalar, or a list of the same length asi
the value of y
is assigned to x
at indexes i
.
Indexed assignment cannot change the type of x
.
If x
is a vector (has negative type) then (=). abs type each(x;y)
must be true.
Where x
is a dictionary, assignment has upsert semantics.
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 op:[x i;y]
Where
op
is a binary operator with infix syntaxx
is an applicable value (i.e. not an atom) in the left domain ofop
i
is a value that indexesx
y
is a value in the right domain ofop
that conforms to eitheri
orx
x op y
(orx[i]op y
) has the same type asx
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
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