1.1、算数运算符
运算符 | 运算 | 说明 | 示例 |
---|
+
| a + b
| 加法 | x = 2;
y = x + 3;
|
+a
| 转换为数字 | x = "2";
y = +x;
|
-
| a - b
| 减法 | x = 2;
y = x - 3;
|
-a
| 取相反数 | x = 2;
y = -x;
|
*
| a * b
| 乘法 | x = 2;
y = x * 3;
|
/
| a / b
| 除法 | x = 2;
y = x / 3;
|
%
| a % b
| 取余 | x = 2;
y = x % 3;
|
^
| a ^ b
| 密方 | x = 2;
y = x ^ 3;
|
**
| a ** b
| x = 2;
y = x ** 3;
|
1.2、自增自减运算符
++
| a++
| 后自增 | x = 2;
y = x++;
|
++a
| 先自增 | x = 2;
y = ++x;
|
--
| a--
| 后自减 | x = 2;
y = x--;
|
--a
| 先自减 | x = 2;
y = --x;
|
1.3、赋值运算符
运算符 | 运算 | 相当于 | 示例 |
---|
=
| a = VALUE
| | x = 2;
y = x + 3;
|
+=
| a += b
| a = a + b
| x = 2;
x += 3;
|
-=
| a -= b
| a = a - b
| x = 2;
x -= 3;
|
*=
| a *= b
| a = a * b
| x = 2;
x *= 3;
|
/=
| a /= b
| a = a / b
| x = 2;
x /= 3;
|
%=
| a %= b
| a = a % b
| x = 2;
x %= 3;
|
^=
| a ^= b
| a = a ^ b
| x = 2;
x ^= 3;
|
**=
| a **= b
| a = a ** b
| x = 2;
x **= 3;
|
1.4、比较运算符
运算符 | 运算 | 说明 | 示例 |
---|
==
| a == b
| 值 等于 | 0 == 0 //true
0 == '0' //true
|
!=
| a != b
| 值 不等于 | 0 != 0 //false
0 != '0' //false
|
<
| a < b
| 小于 | 1 < 2 //true
'0' < 1 //true
|
<=
| a <= b
| 小于或等于 | 2 <= 2 //true
'2' <= 2 //true
|
>
| a > b
| 大于 | 1 > 2 //false
'0' > 1 //false
|
>=
| a >= b
| 大于或等于 | 2 >= 2 //true
'2' >= 2 //true
|
~
| a ~ /regex/
| 匹配正则表达式 | 1 ~ /1/ //true
"1" ~ /1/ //true
|
!~
| a !~ /regex/
| 不匹配正则表达式 | 1 !~ /[a-z]/ //true
"0" !~ /[1-9]/ //true
|
1.5、逻辑运算符
运算符 | 运算 | 说明 | 示例 |
---|
&&
| a && b
| 逻辑与 | 1 && "" //false
1 && "0" //true
0 && "1" //false
1 && 0 //false
|
||
| a || b
| 逻辑或 | 1 || 0 //true
0 || 1 //true
1 || "" //true
0 || "" //false
|
!
| !a
| 逻辑非 | !null //true
!untyped //true
!0 //true
!"" //true
|
1.6、条件运算符
语法:
condition ? expression1 : expression2
示例:
y = x > 0 ? 1 : 0
1.7、其他运算符
运算符 | 运算 | 说明 | 示例 |
---|
in | index in array
| 判断index 是否是array 的索引 | 1 in ARGV //true
"PATH" in ENVIRON //true
|
> | print STRING > "FILE"
printf ITEMS > "FILE"
| 输出重定向,覆盖 | print "hello" > "/dev/null"
printf "%s\n", "hello" > "/dev/null"
|
>> | print STRING >> "FILE"
printf ITEMS >> "FILE"
| 输出重定向,追加 | print "hello" >> "/dev/null"
printf "%s\n", "hello" >> "/dev/null"
|
| | print STRING | "COMMAND"
printf ITEMS | "COMMAND"
| 输出作为其他命令的输入 | print "hello" | "tr [a-z] [A-Z]"
printf "%s\n", "hello" | "tr [a-z] [A-Z]"
|
|& | print STRING |& "COMMAND"
printf ITEMS |& "COMMAND"
| 输出作为其他命令的输入 | print "hello" |& getline var
printf "%s\n", "hello" |& getline var
|
1.8、运算符的优先级