User Tools

Site Tools


expression

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
expression [2014/07/23 11:44]
l
expression [2017/10/10 00:39] (current)
Line 1: Line 1:
 +<- [[display|Displaying A Passage Within Another]] --------- [[function|About Functions]]->​
 +
 ===== About Expressions ===== ===== About Expressions =====
  
Line 23: Line 25:
 | ''​%'' ​   | Modulo (remainder of a division). | ''​5 % 26''​ (is 1) | | ''​%'' ​   | Modulo (remainder of a division). | ''​5 % 26''​ (is 1) |
 | ''​(''​ and ''​)'' ​   | Brackets/​parentheses (causes an expression to be evaluated earlier). | ''​(5 + 10) * 2''​ (is 30, not 25) | | ''​(''​ and ''​)'' ​   | Brackets/​parentheses (causes an expression to be evaluated earlier). | ''​(5 + 10) * 2''​ (is 30, not 25) |
-''​<<'' ​   | Left shift (repeatedly multiplies the left number by 2, as many times as the right number). | ''​5 << ​3''​ (is 40equivalent to ''​* 2 * 2 * 2''​) | + 
-| ''>>'' ​   | Right shift (repeatedly divides the left number by 2as many times as the right number). | ''​40 >> ​3'' ​(is 5equivalent to ''​40 / / 2 / 2''​) | +==== A note about modulo ==== 
-| ''​~'' ​   | Two's complement (adds 1then negates ​the number) ​''​~5'' ​(is -6|+ 
 +Modulo may seem somewhat obtuse an operator, but think of it like this: if you had a sequence of numbers: ​''​0, 1, 2, 3, 4, 5, 6...'', ​and you did ''​3'' ​to each of themthey would become ​''​0, 1, 2, 0, 1, 2, 0...'' ​- that isa constantly looping sequence. This ability to simplify rising sequences allows you to perform some otherwise complicated calculations easily. See the [[function#​visited_string_string|visited()]] function for one such example. 
 + 
 +==== Strings of text ====
  
 You can also use **strings** in an expression. A string is a bunch of characters strung together, demarcated by matching pairs of either double or single quotes. You can use strings in expressions:​ You can also use **strings** in an expression. A string is a bunch of characters strung together, demarcated by matching pairs of either double or single quotes. You can use strings in expressions:​
  
 <​code>​ <​code>​
-<<​print "​The"​ + ' former ' + "Prime Minister">>​+<<​print "​The"​ + ' former ' + "Prime Minister's">>​
 </​code>​ </​code>​
  
-This expression pushes the strings together, and evaluates to "The former Prime Minister"​. Notice that spaces had to be added between the words in order to produce a properly spaced final string. Also, notice that you can only add strings together. You can't subtract them, much less multiply or divide them.+This expression pushes the strings together, and evaluates to "The former Prime Minister's". Notice that spaces had to be added between the words in order to produce a properly spaced final string. Also, notice that you can only add strings together. You can't subtract them, much less multiply or divide them. Finally, notice that the apostrophe in "Prime Minister'​s"​ couldn'​t be included if that string had used single quotes, as they are the same character. (This is why both sets of quotes are permitted to build strings.)
  
 ==== A note about strings and numbers ==== ==== A note about strings and numbers ====
Line 49: Line 54:
 Strings can thus be considered a "​contagious"​ data type - when they are added to other data types, the other data type becomes a string. Strings can thus be considered a "​contagious"​ data type - when they are added to other data types, the other data type becomes a string.
  
-(One upshot of this is that you can convert a number to a string by simply adding the "empty string"​ to it - a string with zero characters, just two quotation marks with nothing between:+(One upshot of this is that you can **convert** a number to a string by simply adding the "empty string"​ to it - a string with zero characters, just two quotation marks with nothing between:
  
 <​code>​ <​code>​
Line 56: Line 61:
  
 Usually, though, there isn't much call for this - numbers are generally as useful as strings.) Usually, though, there isn't much call for this - numbers are generally as useful as strings.)
 +
 +==== Printing Lists ====
 +
 +If you have a list, you can print the contents separated by a comma like so:
 +
 +    <<set $myarray = ["​this",​ "​that"​]>>​
 +    <<​print $myarray.join(",​ "​)>>​
 +
 +This will print the following:
 +
 +     ​this,​that
 +
 +
 +If you have specific questions about using lists in Twine, the [[frequently_asked_questions#​how_do_i_get_things_in_and_out_of_a_list_variable|FAQ]] may help.
 +
  
 ==== Functions ==== ==== Functions ====
Line 126: Line 146:
 ^ Example of value    ^ Description ^ ^ Example of value    ^ Description ^
 | "Some text", "​2",​ "​true",​ 'More text', '​4',​ '​true'​ | **Strings**:​ snippets of text characters that can be [[<<​print>>​]]ed,​ joined up, or compared. Note that "​2"​ and "​true"​ resemble other types but are nonetheless strings. | | "Some text", "​2",​ "​true",​ 'More text', '​4',​ '​true'​ | **Strings**:​ snippets of text characters that can be [[<<​print>>​]]ed,​ joined up, or compared. Note that "​2"​ and "​true"​ resemble other types but are nonetheless strings. |
-| 0, 2, 5, -11, 45.25 | **Numbers** that can be used in arithmetic calculations. | +| 0, 2, 5, -11, 45.25, Infinity ​| **Numbers** that can be used in arithmetic calculations. ​''​Infinity''​ isn't really a number, but has the special property that it's always ''>''​ and ''>​=''​ every actual number, so you can use it in that case. (The same applies to ''​-Infinity''​ and ''<''/''<​=''​) ​
-| true, false | **Logical values**, created using the logical operators, and commonly used with to the [[<<​if>>​]] macro. True and false are the only values of this type. |+| true, false | **Logical values**, created using the logical operators, and commonly used with to the [[<<​if>>​]] macro. True and false are the only values of this type. 
 + 
 +<- [[display|Displaying A Passage Within Another]] --------- [[function|About Functions]]->​
expression.1406115891.txt.gz · Last modified: 2017/10/10 00:37 (external edit)