Array Manipulation

Overview

BattleScript provides a set of functions for working with arrays, allowing you to manipulate collections of data efficiently. These functions help with common operations like adding, removing, finding, and transforming array elements.

Array Creation and Access

Array Declaration

Description: Creates a new array with optional initial values.

Syntax: type[] arrayName = [value1, value2, ...]

Example:

// Create an empty array of integers
array scores = [];

// Create an array with initial values
string[] playerNames = ["Player1", "Player2", "Player3"];

// Create a typed array of custom objects
Player[] players = [];

Array Access

Description: Accesses elements in an array by index.

Syntax: arrayName[index]

Example:

// Access the first element (index 0)
string[] names = ["Alice", "Bob", "Charlie"];
string firstPlayer = names[0];  // Returns "Alice"

// Set a value at a specific index
names[1] = "Bobby";  // Changes "Bob" to "Bobby"

// Access the last element
string lastPlayer = names[names.length - 1];  // Returns "Charlie"

array.length

Description: Gets the number of elements in an array.

Syntax: arrayName.length

Returns: The number of elements in the array

Example:



int[] scores = [85, 92, 78, 90];
int count = scores.length;  // Returns 4

// Use length in a loop
for (int i = 0; i < scores.length; i++) {
    std::console.log("Score", i, ":", scores[i]);
}

Adding and Removing Elements

array.push

Description: Adds one or more elements to the end of an array.

Syntax: arrayName.push(element1, element2, ...)

Parameters:

  • element1, element2, ... - The elements to add to the array

Returns: The new length of the array

Example:

// Add a single element
string[] fruits = ["apple", "banana"];
fruits.push("orange");  // fruits is now ["apple", "banana", "orange"]

// Add multiple elements
fruits.push("grape", "kiwi");  // fruits is now ["apple", "banana", "orange", "grape", "kiwi"]

array.pop

Description: Removes the last element from an array and returns that element.

Syntax: arrayName.pop()

Returns: The removed element

Example:

string[] stack = ["first", "second", "third"];
string lastItem = stack.pop();  // Returns "third", stack is now ["first", "second"]

// Use pop to implement a stack
stack.push("new item");  // stack is now ["first", "second", "new item"]
string item = stack.pop();  // Returns "new item", stack is back to ["first", "second"]

array.unshift

Description: Adds one or more elements to the beginning of an array.

Syntax: arrayName.unshift(element1, element2, ...)

Parameters:

  • element1, element2, ... - The elements to add to the array

Returns: The new length of the array

Example:

string[] queue = ["second", "third"];
queue.unshift("first");  // queue is now ["first", "second", "third"]

// Add multiple elements at the beginning
queue.unshift("zero", "pre-first");  // queue is now ["zero", "pre-first", "first", "second", "third"]

array.shift

Description: Removes the first element from an array and returns that element.

Syntax: arrayName.shift()

Returns: The removed element

Example:

string[] queue = ["first", "second", "third"];
string firstItem = queue.shift();  // Returns "first", queue is now ["second", "third"]

// Use shift to implement a queue
queue.push("new item");  // queue is now ["second", "third", "new item"]
string nextItem = queue.shift();  // Returns "second", queue is now ["third", "new item"]

array.splice

Description: Changes the contents of an array by removing or replacing existing elements and/or adding new elements.

Syntax: arrayName.splice(int start, int deleteCount, item1, item2, ...)

Parameters:

  • start - The index at which to start changing the array
  • deleteCount - The number of elements to remove
  • item1, item2, ... - The elements to add to the array (optional)

Returns: An array containing the deleted elements

Example:

// Remove elements
string[] colors = ["red", "green", "blue", "yellow", "purple"];
colors.splice(1, 2);  // Removes "green" and "blue", colors is now ["red", "yellow", "purple"]

// Replace elements
string[] fruits = ["apple", "banana", "orange"];
fruits.splice(1, 1, "kiwi");  // Replaces "banana" with "kiwi", fruits is now ["apple", "kiwi", "orange"]

// Insert elements without removing any
int[] numbers = [1, 2, 5];
numbers.splice(2, 0, 3, 4);  // Inserts 3 and 4 at index 2, numbers is now [1, 2, 3, 4, 5]

Searching and Filtering

array.indexOf

Description: Returns the first index at which a given element can be found in the array, or -1 if it is not present.

Syntax: CALL arrayName.indexOf(element)

Parameters:

  • element - The element to locate in the array

Returns: The first index of the element in the array, or -1 if not found

Example:



string[] fruits = ["apple", "banana", "orange", "banana"];
int index = fruits.indexOf("banana");  // Returns 1

// Check if an element exists in the array
int index2 = fruits.indexOf("grape");  // Returns -1
if (index2 == -1) {
    std::console.log("Grape not found in the array");
}

array.includes

Description: Determines whether an array includes a certain element, returning true or false.

Syntax: CALL arrayName.includes(element)

Parameters:

  • element - The element to search for

Returns: true if the element is found, false otherwise

Example:

string[] allowedWeapons = ["pistol", "rifle", "shotgun"];

// Check if a weapon is allowed
string playerWeapon = "rifle";
if (allowedWeapons.includes(playerWeapon)) {
    serial.log("Weapon is allowed");
} else {
    serial.log("Weapon is not allowed");
}

array.filter

Description: Creates a new array with all elements that pass the test implemented by the provided function.

Syntax: arrayName.filter(function callback)

Parameters:

  • callback - Function that tests each element; return true to keep the element, false otherwise

Returns: A new array with the elements that pass the test

Example:

int[] scores = [75, 90, 45, 88, 62, 95];

// Filter scores above 80
int[] highScores = scores.filter((score) => {
    return score > 80;
});  // highScores is [90, 88, 95]

// Filter active players
Player[] allPlayers = [player1, player2, player3, player4];
Player[] activePlayers = allPlayers.filter((player) => {
    return player.isActive == true;
});

Related Topics