184
edits
| Tomas Kirnak (talk | contribs)   (Clarified array creation and usage) | |||
| Line 223: | Line 223: | ||
| == Array Initialization == | == Array Initialization == | ||
| New array can be created in two ways. First is to use '''array''' operator | New array can be created in two ways. First is to use '''array''' operator. <br> | ||
| This statement will create empty array and assign reference to it to variable ''a''. | |||
| <syntaxhighlight lang="c"> | |||
| array a; | |||
| </syntaxhighlight> | |||
| You can then assign values to the array like this.<br> | |||
| Please note arrays in NXSL are sparse, so you can have elements with nothing in between. | |||
| <syntaxhighlight lang="c"> | <syntaxhighlight lang="c"> | ||
| array a; | array a; | ||
| a[1] = 1; | |||
| a[2] = 2; | |||
| a[260] = 260; | |||
| println(a[1]); // will print 1 | |||
| </syntaxhighlight> | </syntaxhighlight> | ||
| Second way is to use %( ) construct to create array already populated with values.<br> | |||
| This statement will create array with four elements at positions 0, 1, 2, and 3, and assign reference to this array to variable ''a''. | |||
| <syntaxhighlight lang="c"> | <syntaxhighlight lang="c"> | ||
| // no need to use "array a;" here, since we are creating it dirrectly | |||
| a = %(1, 2, 3, 4); | a = %(1, 2, 3, 4); | ||
| println(a[0]); // will actually print 1, since 1 is the 0th member | |||
| </syntaxhighlight> | </syntaxhighlight> | ||
| Array initialization can also be used directly in expressions, like this: | |||
| <syntaxhighlight lang="c"> | <syntaxhighlight lang="c"> | ||
edits