Nasal scripting language: Difference between revisions

Jump to navigation Jump to search
Line 191: Line 191:


'''''More information can be found at [[Namespaces and Methods]].'''''
'''''More information can be found at [[Namespaces and Methods]].'''''
== Variables - Advanced Uses ==
Nasal, also supports Multi-assignment expressions. You can assign more than one variable (or lvalue) at a time by putting them in a parenthesized list:
<syntaxhighlight lang="php">
  (var a, var b) = (1, 2);
  var (a, b) = (1, 2);              # Shorthand for (var a, var b)
  (var a, v[0], obj.field) = (1,2,3) # Any assignable lvalue works
  var color = [1, 1, 0.5];
  var (r, g, b) = color;  # works with runtime vectors too
</syntaxhighlight>
Vectors (lists or arrays) can be created from others using an ordered list of indexes and ranges.
This is usually called "vector slicing".
For example:
<syntaxhighlight lang="php">
  var v1 = ["a","b","c","d","e"]
  var v2 = v1[3,2];  # == ["d","c"];
  var v3 = v1[1:3];  # i.e. range from 1 to 3: ["b","c","d"];
  var v4 = v1[1:];    # no value means "to the end": ["b","c","d","e"]
  var i = 2;
  var v5 = v1[i];    # runtime expressions are fine: ["c"]
  var v6 = v1[-2,-1]; # negative indexes are relative to end: ["d","e"]
</syntaxhighlight>
The range values can be computed at runtime (e.g. i=1; v5=v1[i:]). Negative indices work the same way they do with the vector functions (-1 is the last element, -2 is 2nd to last, etc...).


== Storage: property tree vs. Nasal ==
== Storage: property tree vs. Nasal ==

Navigation menu