github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/gen/includes/math.inc.md (about)

     1  ### Variables
     2  
     3  There are two ways you can use variables with the math functions. Either by
     4  string interpolation like you would normally with any other function, or
     5  directly by name.
     6  
     7  String interpolation:
     8  
     9  ```
    10  » set abc=123
    11  » = $abc==123
    12  true
    13  ```
    14  
    15  Directly by name:
    16  
    17  ```
    18  » set abc=123
    19  » = abc==123
    20  false
    21  ```
    22  
    23  To understand the difference between the two, you must first understand how
    24  string interpolation works; which is where the parser tokenised the parameters
    25  like so
    26  
    27  ```
    28  command line: = $abc==123
    29  token 1: command (name: "=")
    30  token 2: parameter 1, string (content: "")
    31  token 3: parameter 1, variable (name: "abc")
    32  token 4: parameter 1, string (content: "==123")
    33  ```
    34  
    35  Then when the command line gets executed, the parameters are compiled on demand
    36  similarly to this crude pseudo-code
    37  
    38  ```
    39  command: "="
    40  parameters 1: concatenate("", GetValue(abc), "==123")
    41  output: "=" "123==123"
    42  ```
    43  
    44  Thus the actual command getting run is literally `123==123` due to the variable
    45  being replace **before** the command executes.
    46  
    47  Whereas when you call the variable by name it's up to `=` or `let` to do the
    48  variable substitution.
    49  
    50  ```
    51  command line: = abc==123
    52  token 1: command (name: "=")
    53  token 2: parameter 1, string (content: "abc==123")
    54  ```
    55  
    56  ```
    57  command: "="
    58  parameters 1: concatenate("abc==123")
    59  output: "=" "abc==123"
    60  ```
    61  
    62  The main advantage (or disadvantage, depending on your perspective) of using
    63  variables this way is that their data-type is preserved.
    64  
    65  ```
    66  » set str abc=123
    67  » = abc==123
    68  false
    69  
    70  » set int abc=123
    71  » = abc==123
    72  true
    73  ```
    74  
    75  Unfortunately is one of the biggest areas in Murex where you'd need to be
    76  careful. The simple addition or omission of the dollar prefix, `$`, can change
    77  the behavior of `=` and `let`.
    78  
    79  ### Strings
    80  
    81  Because the usual Murex tools for encapsulating a string (`"`, `'` and `()`)
    82  are interpreted by the shell language parser, it means we need a new token for
    83  handling strings inside `=` and `let`. This is where backtick comes to our
    84  rescue.
    85  
    86  ```
    87  » set str abc=123
    88  » = abc==`123`
    89  true
    90  ```
    91  
    92  Please be mindful that if you use string interpolation then you will need to
    93  instruct `=` and `let` that your field is a string
    94  
    95  ```
    96  » set str abc=123
    97  » = `$abc`==`123`
    98  true
    99  ```
   100  
   101  ### Best practice recommendation
   102  
   103  As you can see from the sections above, string interpolation offers us some
   104  conveniences when comparing variables of differing data-types, such as a `str`
   105  type with a number (eg `num` or `int`). However it makes for less readable code
   106  when just comparing strings. Thus the recommendation is to avoid using string
   107  interpolation except only where it really makes sense (ie use it sparingly).
   108  
   109  ### Non-boolean logic
   110  
   111  Thus far the examples given have been focused on comparisons however `=` and
   112  `let` supports all the usual arithmetic operators:
   113  
   114  ```
   115  » = 10+10
   116  20
   117  
   118  » = 10/10
   119  1
   120  
   121  » = (4 * (3 + 2))
   122  20
   123  
   124  » = `foo`+`bar`
   125  foobar
   126  ```
   127  
   128  ### Read more
   129  
   130  Murex uses the [govaluate package](https://github.com/Knetic/govaluate). More information can be found in it's manual.