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 revision Previous revision
Next revision
Previous revision
script:walkthrough:basic_expressions [2019-11-19 21:59]
skyjake [Constants]
script:walkthrough:basic_expressions [2019-11-20 07:30] (current)
skyjake
Line 3: Line 3:
 ====== Basic expressions ====== ====== Basic expressions ======
  
-===== Basic types =====+===== Numbers and strings ​=====
  
 All numbers are internally stored as 64-bit floats. All numbers are internally stored as 64-bit floats.
Line 20: Line 20:
   Underscores for readability:​ 0x123456   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 without having to escape them with a backslash.+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'​   $ print "​Hello",​ '​World'​
   Hello World   Hello World
   ​   ​
-  $ print """​I can span+  $ print """​I can "span"
   newlines."""​   newlines."""​
-  I can span+  I can "span"
   newlines.   newlines.
 +
 +
 +===== Arrays and dictionaries =====
  
 Arrays are created using brackets around comma-separated elements. Arrays are created using brackets around comma-separated elements.
Line 45: Line 48:
   { a: 10, b: 5 }   { a: 10, b: 5 }
  
-Both the key and the value can be of any type. +Keys and values ​can be of any type. 
   ​   ​
   $ print {'​a':​ '​b',​ 1: ['​b',​ {5:6, 6:7}], ['​array',​ '​as',​ '​key'​]:​ 'oh my'}   $ 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 }   { 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. Time values store a specific point in time. The built-in function ''​Time()''​ returns the current local time.
Line 54: Line 60:
   $ print 'The time is now:', Time()   $ print 'The time is now:', Time()
   The time is now: 2019-11-19 21:​33:​16.501   The time is now: 2019-11-19 21:​33:​16.501
 +
  
 ===== Constants ===== ===== Constants =====
  
-Commonly used constants have reserved keywords.+There are reserved keywords ​for commonly used constants.
  
   $ print True, False, None, Pi   $ print True, False, None, Pi
   True False (none) 3.14159   True False (none) 3.14159
 +
 +
 ===== Built-in functions ===== ===== Built-in functions =====
  
-  *  Built-in functions: ​len(), dictkeys(), dictvalues(). +The built-in ''​len()''​ function returns the size/length of its argument.
-  *  Built-in functions: Text(), Number(). +
-  *  Built-in functions: serialize() and deserialize().+
  
 +  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
script/walkthrough/basic_expressions.1574193592.txt.gz · Last modified: 2019-11-19 21:59 by skyjake