Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
script:walkthrough:operator_expressions [2019-11-20 06:45] – skyjake | script:walkthrough:operator_expressions [2019-11-20 10:24] (current) – skyjake | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | <- Basic expressions ^ ^ Statements and compounds -> | ||
+ | |||
+ | ====== Operator expressions ====== | ||
+ | |||
+ | ===== Addition ===== | ||
+ | |||
+ | $ print ' | ||
+ | Numbers: 2 0 | ||
+ | | ||
+ | $ print ' | ||
+ | 0xffffffffff + 0xffffffffff | ||
+ | Larger than 32-bit: 2.19902e+12 | ||
+ | | ||
+ | $ print ' | ||
+ | Text: HelloWorld | ||
+ | | ||
+ | $ print ' | ||
+ | Array: [ 1, 2, 3, a, b, c ] | ||
+ | | ||
+ | $ print ' | ||
+ | {1:2, 3:4} + {' | ||
+ | Dictionary: { 1: 2, 3: 4, a: b, c: d } | ||
+ | | ||
+ | $ print ' | ||
+ | Time: 2019-11-19 22: | ||
+ | |||
+ | ===== Subtraction ===== | ||
+ | |||
+ | $ print ' | ||
+ | Numbers: 0 -1 | ||
+ | | ||
+ | $ print ' | ||
+ | Larger than 32-bit: 0 | ||
+ | | ||
+ | $ print ' | ||
+ | Dictionary: { b: B } | ||
+ | | ||
+ | $ print ' | ||
+ | Time: 2019-11-19 20: | ||
+ | |||
+ | ===== Multiplication ===== | ||
+ | |||
+ | $ print ' | ||
+ | Numbers: 2 10 360 | ||
+ | | ||
+ | $ print ' | ||
+ | Text: WordWordWord LongwordLongwordLongwordLong | ||
+ | |||
+ | ===== Division/ | ||
+ | |||
+ | All numbers are internally floating point, so the result of integer division is not forced to be an integer. | ||
+ | |||
+ | $ print ' | ||
+ | Numbers: 1 2.5 inf | ||
+ | |||
+ | The ''/'' | ||
+ | |||
+ | $ print 'Path concatenation:', | ||
+ | ' | ||
+ | ' | ||
+ | Path concatenation: | ||
+ | |||
+ | If the right-hand argument begins with a slash (absolute path), the left-hand argument is ignored as the absolute path overrides it. | ||
+ | |||
+ | $ print ' | ||
+ | Absolute: / | ||
+ | |||
+ | ===== Modulo ===== | ||
+ | |||
+ | Modulo converts number arguments to integers. | ||
+ | |||
+ | $ print ' | ||
+ | Numbers: 1 | ||
+ | |||
+ | '' | ||
+ | |||
+ | $ print '%s with " | ||
+ | ' | ||
+ | Formatted with " | ||
+ | | ||
+ | $ print 'Width and precision: " | ||
+ | Width and precision: " | ||
+ | |||
+ | ===== Combined assignment operators ===== | ||
+ | |||
+ | Combined assignment operators work on existing variables only. They are considered expressions, | ||
+ | |||
+ | $ a = 1 | ||
+ | | ||
+ | $ print 'a =', a += 3 | ||
+ | a = 4 | ||
+ | | ||
+ | $ print 'a =', a -= 3 | ||
+ | a = 1 | ||
+ | | ||
+ | $ print 'a =', a *= 100 | ||
+ | a = 100 | ||
+ | | ||
+ | $ print 'a =', a /= 10 | ||
+ | a = 10 | ||
+ | | ||
+ | $ print 'a =', a %= 3 | ||
+ | a = 1 | ||
+ | | ||
+ | $ path = '/ | ||
+ | | ||
+ | $ print 'path =', path | ||
+ | path = /some/path/ | ||
+ | | ||
+ | $ print 'path =', path /= ' | ||
+ | path = / | ||
+ | |||
+ | ===== Subscript ===== | ||
+ | |||
+ | The subscript operator '' | ||
+ | |||
+ | $ transports = [' | ||
+ | | ||
+ | $ print transports[2] | ||
+ | automobiles | ||
+ | |||
+ | Negative indices are counted from the end of the array. | ||
+ | |||
+ | $ print transports[-1] | ||
+ | bicycles | ||
+ | |||
+ | The subscript operator is also used to access dictionaries by key. | ||
+ | |||
+ | $ xlat = {' | ||
+ | | ||
+ | $ print xlat[' | ||
+ | hello world | ||
+ | |||
+ | Arrays can be nested. | ||
+ | |||
+ | $ matrix = [[1, 2], [3, 4]] | ||
+ | | ||
+ | $ print ' | ||
+ | Two-level indexing: 2 4 | ||
+ | | ||
+ | $ matrix2 = [matrix, matrix] | ||
+ | | ||
+ | $ print matrix2[1][1][0], | ||
+ | 3 from [ [ [ 1, 2 ], [ 3, 4 ] ], [ [ 1, 2 ], [ 3, 4 ] ] ] | ||
+ | |||
+ | ===== Slice ===== | ||
+ | |||
+ | The slice operator is versatile tool for extracting a subset from an array or a text string. It takes a range of indices as argument. If the start of the slice range is omitted, the slice begins from the start of the array. Omitting the end of the range works similarly. | ||
+ | |||
+ | $ fullArray = [1, 2, 3, 4, 5, 6, 7] | ||
+ | | ||
+ | $ print ' | ||
+ | Sliced: [ 3, 4 ] [ 4, 5, 6, 7 ] [ 1, 2, 3 ] | ||
+ | |||
+ | Slice arguments can be expressions. | ||
+ | |||
+ | $ print fullArray[0: | ||
+ | [ 1, 2, 3, 4, 5, 6, 7 ] | ||
+ | |||
+ | The slice range may use negative indices, too. | ||
+ | |||
+ | $ print ' | ||
+ | Nega-sliced: | ||
+ | |||
+ | An optional third parameter can be provided to specify the step size within the slice range. | ||
+ | |||
+ | $ print ' | ||
+ | Stepping: [ 1, 3, 5, 7 ] [ 2, 4, 6 ] | ||
+ | |||
+ | The step value can be negative to reverse the resulting slice. | ||
+ | |||
+ | $ print ' | ||
+ | Reverse slice: [ 5, 4 ] | ||
+ | |||
+ | The slice operator can therefore be used to easily reverse an array. | ||
+ | |||
+ | $ print ' | ||
+ | [ 7, 6, 5, 4, 3, 2, 1 ] | ||
+ | |||
+ | In addition to arrays, slices work on text strings as well. | ||
+ | |||
+ | $ fullString = " | ||
+ | | ||
+ | $ print ' | ||
+ | Sliced: cd defg abc | ||
+ | | ||
+ | $ print ' | ||
+ | Nega-sliced: | ||
+ | | ||
+ | $ print ' | ||
+ | Stepping: aceg bdf | ||
+ | | ||
+ | $ print ' | ||
+ | Reverse slice: ed | ||
+ | | ||
+ | $ print ' | ||
+ | Reversing: gfedcba | ||
+ | |||
+ | ===== Logical operators ===== | ||
+ | |||
+ | The '' | ||
+ | |||
+ | $ print not True | ||
+ | False | ||
+ | | ||
+ | $ print not False | ||
+ | True | ||
+ | |||
+ | A non-empty text string is considered a boolean truth, so negation yields False. | ||
+ | |||
+ | $ print not 'Is this true?' | ||
+ | False | ||
+ | |||
+ | '' | ||
+ | |||
+ | $ print True and False | ||
+ | False | ||
+ | | ||
+ | $ print True or False | ||
+ | True | ||
+ | |||
+ | '' | ||
+ | |||
+ | $ print True or True and False | ||
+ | True | ||
+ | |||
+ | Early termination applies to both operators. | ||
+ | |||
+ | # Helper function to see what gets evaluated. | ||
+ | def showValue(a) | ||
+ | print ' | ||
+ | return a | ||
+ | end | ||
+ | | ||
+ | $ showValue(True) and showValue(True) | ||
+ | called with True | ||
+ | called with True | ||
+ | | ||
+ | $ showValue(False) and showValue(True) | ||
+ | called with False | ||
+ | | ||
+ | $ showValue(False) or showValue(True) | ||
+ | called with False | ||
+ | called with True | ||
+ | | ||
+ | $ showValue(True) or showValue(True) | ||
+ | called with True | ||
+ | |||
+ | |||
+ | ===== Comparison ===== | ||
+ | |||
+ | The usual set of comparison operators is available. | ||
+ | |||
+ | $ print ' | ||
+ | 2 <= 2, 4 <= 2 | ||
+ | Numbers: False True True True True False | ||
+ | |||
+ | Text string comparison is case sensitive. | ||
+ | |||
+ | $ print ' | ||
+ | ' | ||
+ | Text: False True True | ||
+ | | ||
+ | $ print ' | ||
+ | True/False: True True | ||
+ | |||
+ | With arrays, the comparison is done for each element starting from the first one, until a difference is found. | ||
+ | | ||
+ | $ print ' | ||
+ | Arrays: True True False True False | ||
+ | |||
+ | Arrays of different lengths are compared solely based on the lengths. | ||
+ | |||
+ | $ print [100] < [1, 2] | ||
+ | True | ||
+ | |||
+ | ===== Membership ===== | ||
+ | |||
+ | The '' | ||
+ | |||
+ | $ transports = [' | ||
+ | | ||
+ | $ print ' | ||
+ | True | ||
+ | | ||
+ | $ print ' | ||
+ | False | ||
+ | |||
+ | '' | ||
+ | |||
+ | $ print ' | ||
+ | False | ||
+ | | ||
+ | $ print ' | ||
+ | True | ||