User Tools

Site Tools


script:walkthrough:basic_expressions

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
script:walkthrough:basic_expressions [2019-11-20 05:19] – [Numbers and strings] skyjakescript:walkthrough:basic_expressions [2019-11-20 05:30] (current) skyjake
Line 1: Line 1:
 +<- script:reference|Reference guide ^ ^ Operator expressions ->
  
 +====== Basic expressions ======
 +
 +===== Numbers and strings =====
 +
 +All numbers are internally stored as 64-bit floats.
 +
 +  $ print 5, 5.5, -3.141592657
 +  5 5.5 -3.14159
 +
 +Hexadecimal numbers use the "0x" prefix.
 +
 +  $ print 0x100, 0X123
 +  256 291
 +
 +Underscores can be inserted in numbers at any points. This is only to improve readability for humans; the parser ignores them.
 +
 +  $ print 'Underscores for readability: %x' % 0x0012_3456
 +  Underscores for readability: 0x123456
 +  
 +Text strings can use single, double, or triple quotes. Single and double are semantically equal. C-like escape sequences are supported, for example ''\n'' for a newline. Triple-quoted strings can contain newlines and quotes without having to escape them with a backslash.
 +
 +  $ print "Hello", 'World'
 +  Hello World
 +  
 +  $ print """I can "span"
 +  newlines."""
 +  I can "span"
 +  newlines.
 +
 +
 +===== Arrays and dictionaries =====
 +
 +Arrays are created using brackets around comma-separated elements.
 +
 +  $ print [1, 2, 3]
 +  [ 1, 2, 3 ]
 +
 +The elements of arrays can be of any type.
 +
 +  $ print [1, [2, 3], 4]
 +  [ 1, [ 2, 3 ], 4 ]
 +
 +Dictionaries contain key-value pairs. A dictionary is created with curly brackets. 
 +
 +  $ print {'a': 10, 'b': 5}
 +  { a: 10, b: 5 }
 +
 +Keys and values can be of any type. 
 +  
 +  $ print {'a': 'b', 1: ['b', {5:6, 6:7}], ['array', 'as', 'key']: 'oh my'}
 +  { 1: [ b, { 5: 6, 6: 7 } ], [ array, as, key ]: oh my, a: b }
 +
 +
 +===== Other value types =====
 +
 +Time values store a specific point in time. The built-in function ''Time()'' returns the current local time.
 +
 +  $ print 'The time is now:', Time()
 +  The time is now: 2019-11-19 21:33:16.501
 +
 +
 +===== Constants =====
 +
 +There are reserved keywords for commonly used constants.
 +
 +  $ print True, False, None, Pi
 +  True False (none) 3.14159
 +
 +
 +===== Built-in functions =====
 +
 +The built-in ''len()'' function returns the size/length of its argument.
 +
 +  len("abcd") ⇒ 4
 +  len([1, 2, 3, 4]) ⇒ 4
 +  len({1:2, 3:5, 8:13}) ⇒ 3
 +
 +There are functions for extracting all the keys or all the values of a dictionary into an array.
 +
 +  dictkeys({1:2, 3:5, 8:13}) ⇒ [ 1, 3, 8 ]
 +  dictvalues({1:2, 3:5, 8:13}) ⇒ [ 2, 5, 13 ]
 +
 +The functions ''Text()'' and ''Number()'' convert the argument to a text string or a number.
 +
 +  $ print Text(123), Number("123")
 +  123 123
 +
 +One can give a string argument to the ''Time()'' function to parse as a time and date.
 +
 +  $ print "Beginning of March 2011:", Time("2011-03-01")
 +  Beginning of March 2011: 2011-03-01 00:00:00.000
 +  
 +  $ print Time("2000-01-02 03:04:05.678")
 +  2000-01-02 03:04:05.678
 +
 +If the parsing fails, the resulting value represents an undefined point in time. A valid time evaluates to True and an undefined time evaluates to False when used as a boolean condition.
 +  
 +  $ print 'Invalid time:', Time("123-123"), Time("")
 +  Invalid time: (undefined Time) (undefined Time)
 +
 +The ''timedelta()'' function calculates the difference between two points in time.
 +  
 +  $ print 'Thousand seconds later:', timedelta(Time(), Time() + 1000)
 +  Thousand seconds later: 1000
 +
 +The ''serialize()'' and ''deserialize()'' functions convert their argument to/from a sequence of bytes. This is useful for instance when saving and reading values to/from a file. All values created in scripts can be serialized. The contents of a serialized block of memory cannot be modified via Doomsday Script.
 +
 +  $ a = [1, 'One', {1:'One'}]
 +  
 +  $ print a
 +  [ 1, One, { 1: One } ]
 +  
 +  $ b = serialize(a)
 +  
 +  $ print b
 +  (Block of 46 bytes)
 +  
 +  $ print deserialize(b)
 +  [ 1, One, { 1: One } ]
 +
 +The ''eval()'' function evaluates its argument as a script and returns the return value from it. The script gets runs in the same namespace where ''eval()'' was called.
 +
 +  $ a = eval(""" 'arg' + 'ument' """)
 +  
 +  $ print a
 +  argument
 +  
 +  $ eval("""print "Printed from eval():", a""")
 +  Printed from eval(): argument