Arrays

Rockstar supports JavaScript-style arrays. Arrays are zero-based, and dynamically allocated when values are assigned using numeric indexes. Array elements are initialised to null; passing an out-of-range index returns mysterious:

Absolution at 0 is "Intro"
Absolution at 1 is "Apocalypse Please"
My favourite is 7
Absolution at my favourite is "Hysteria"
Absolution at 2 + 3 is "Stockholm Syndrome"

Print Absolution at 0 (prints: Intro)
Print Absolution at 7 (prints: Hysteria)
Print Absolution at 2 (prints: null)
Print Absolution at 999 (prints: mysterious)

Array indexers can be primary values or arithmetic expressions, but you can’t use a logical expression as an array indexer.

Consider My array at 2 is 4

If not for this restriction, the parser would consume 2 is 4 as a comparison (“2 is 4 - true or false?”), return false, try to set My array at false and then blow up ‘cos there’s nothing to put in it.

Returning an array in a scalar context will return the current length of the array:

Let the array at 5150 be "Van Halen".
Write the array (writes: 5151)

Array indexes can be of any type, and you can mix key types within the same array. The array length only considers keys whose values are non-negative integers:

My string is "string"
My decimal is 1.234
My negative is -4
My boolean is true

The array at my string is "hey"
The array at my decimal is "hey"
The array at my negative is "my"
The array at my boolean is "my"

Print the array (prints: 0)
The array at 0 is "rock & roll will never die"
Print the array (prints: 1)

Arrays in Rockstar are one-dimensional, but they can contain other arrays:

My string is "string"
My decimal is 1.234
My negative is -4
My boolean is true

The array at my string is "hey"
The array at my decimal is "hey"
The array at my negative is "my"
The array at my boolean is "my"

Print the array (prints: 0)
The array at 0 is "rock & roll will never die"
Print the array (prints: 1)

You can use indexes to read characters from strings, and extract bits from numbers. You can also use indexers to modify individual characters in a string:

X is 43605. Index is 0
Until index is 16
If x at index write 1 else write 0
Index is with 1
end (writes: 1010101001010101)

The string is "Han Valen"
The string at 0 is "V"
The string at 4 is "H"
Shout the string (prints: Van Halen)

X is 0. Print X (prints: 0)
X at 0 is true. Print X. (prints: 1)
X at 2 is true. Print X. (prints: 5)
X at 4 is true. Print X. (prints: 21)


Trying to assign an indexed value to an existing variable which is not an array will cause an error:

The truth takes nothing giving nothing
The truth at 1 is 2
(runtime error: The truth is not an indexed variable)

Queue operations

Rockstar arrays can also be created and manipulated by the queue operations rock and roll. (The aliases push and pop are supported for Rockstar developers who are into 80s dance music.)

Pushing elements onto an array

To create a new empty array, push or rock the name of the array. To push an element onto the end of the array, push <array> <expression>.

Rock my array
Print my array (prints: 0)
Rock my array with 123
Print my array (prints: 1)
Print my array at 0 (prints: 123)
Roll my array into the result
Print the result (prints: 123)
Print my array (prints: 0)

You can rock list expressions, so you can push multiple elements onto the end of an array:

Rock Tommy "yeah!". Rock Tommy 12345. Rock Tommy true
Rock Tommy like a renegade razorblade
Rock Tommy with nothing
Rock Tommy with lies
Rock Tommy 5, 6, 7, 8
Shout Tommy (prints: 10)

While Tommy ain't nothing
Roll Tommy into the fire
Write the fire; write ", ", yeah
(writes: yeah!, 12345, true, 180, null, false, 5, 6, 7, 8, )


If it makes for better lyrics, you can use the with keyword - rock <array> with <expression>. Remember the with keyword is context-sensitive, so in this example:

Rock ints with 1, 2 with 3, 4, 5
          ^         ^
          |         +-- this 'with' is the binary addition operator
          |
          +------------ this 'with' is part of the array push syntax
          
(ints is now [ 1, 5, 4, 5 ])

This syntax is very useful for initialising strings without using string literals - see below. It also means that the following is valid Rockstar:

The Scorpions say here I am
Rock you like a hurricane

Shout it at 0
Shout it at 1

Popping elements from an array

The roll keyword will remove the first element from an array and return the element that was removed.

Rock ints with 1, 2, 3
Print roll ints (prints: 1)
Print pop ints (prints: 2)
Print roll ints (prints: 3)
Print pop ints (prints: mysterious)

The string is "abcde"
Roll the string into the letter
Print the letter (prints: a)
Print the string (prints: bcde)

roll can be used in assignments:

Rock ints with 1, 2, 3
Let the first be roll ints
Let the second be roll ints
Let the third be roll ints
Shout the first (prints: 1)
Shout the second (prints: 2)
Shout the third (prints: 3)

Rockstar also supports a special roll x into y syntax for removing the first element from an array and assigning it to a variable:

Rock the list with 4, 5, 6
Roll the list into foo
Roll the list into bar
Roll the list into baz
Shout foo (prints: 4)
Shout bar (prints: 5)
Shout baz (prints: 6)