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-20 07:19]
skyjake
script:walkthrough:basic_expressions [2019-11-20 07:30] (current)
skyjake
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 =====
Line 61: Line 68:
   $ 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 =====
  
Line 97: Line 106:
   Thousand seconds later: 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 the serialized block of memory cannot be modified via Doomsday Script.+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 serialized block of memory cannot be modified via Doomsday Script.
  
   $ a = [1, '​One',​ {1:'​One'​}]   $ a = [1, '​One',​ {1:'​One'​}]
Line 112: Line 121:
   [ 1, One, { 1: One } ]   [ 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.1574227164.txt.gz ยท Last modified: 2019-11-20 07:19 by skyjake