102 lines
2.2 KiB
Plaintext
102 lines
2.2 KiB
Plaintext
|
|
print("=== Arrays Demo ===");
|
||
|
|
print("");
|
||
|
|
|
||
|
|
print("--- Array Literals ---");
|
||
|
|
empty = {};
|
||
|
|
print("Empty array length:", len(empty));
|
||
|
|
|
||
|
|
numbers = {1, 2, 3, 4, 5};
|
||
|
|
print("Numbers:", numbers[0], numbers[1], numbers[2], numbers[3], numbers[4]);
|
||
|
|
print("Length:", len(numbers));
|
||
|
|
|
||
|
|
mixed = {42, "hello", 3.14, null};
|
||
|
|
print("Mixed array:");
|
||
|
|
print(" int:", mixed[0]);
|
||
|
|
print(" str:", mixed[1]);
|
||
|
|
print(" float:", mixed[2]);
|
||
|
|
print(" null:", mixed[3]);
|
||
|
|
|
||
|
|
print("");
|
||
|
|
print("--- Array Indexing ---");
|
||
|
|
fruits = {"apple", "banana", "cherry", "date"};
|
||
|
|
print("First element:", fruits[0]);
|
||
|
|
print("Last element:", fruits[3]);
|
||
|
|
|
||
|
|
print("");
|
||
|
|
print("--- Array Modification ---");
|
||
|
|
arr = {10, 20, 30};
|
||
|
|
print("Before:", arr[0], arr[1], arr[2]);
|
||
|
|
arr[1] = 25;
|
||
|
|
print("After arr[1] = 25:", arr[0], arr[1], arr[2]);
|
||
|
|
|
||
|
|
print("");
|
||
|
|
print("--- Array Methods ---");
|
||
|
|
stack = {};
|
||
|
|
stack.push(1);
|
||
|
|
stack.push(2);
|
||
|
|
stack.push(3);
|
||
|
|
print("Stack length:", len(stack));
|
||
|
|
|
||
|
|
popped = stack.pop();
|
||
|
|
print("Popped:", popped);
|
||
|
|
print("After pop length:", len(stack));
|
||
|
|
|
||
|
|
print("");
|
||
|
|
print("--- Array Join ---");
|
||
|
|
words = {"Hello", "World"};
|
||
|
|
sentence = words.join(" ");
|
||
|
|
print("Joined with space:", sentence);
|
||
|
|
|
||
|
|
nums = {1, 2, 3};
|
||
|
|
csv = nums.join(",");
|
||
|
|
print("Joined with comma:", csv);
|
||
|
|
|
||
|
|
print("");
|
||
|
|
print("--- Array indexOf ---");
|
||
|
|
items = {"apple", "banana", "cherry"};
|
||
|
|
idx = items.indexOf("banana");
|
||
|
|
print("Index of 'banana':", idx);
|
||
|
|
|
||
|
|
idx = items.indexOf("grape");
|
||
|
|
print("Index of 'grape' (not found):", idx);
|
||
|
|
|
||
|
|
print("");
|
||
|
|
print("--- Array Slice ---");
|
||
|
|
source = {0, 1, 2, 3, 4, 5};
|
||
|
|
sliced = source.slice(2, 4);
|
||
|
|
print("slice(2, 4):", sliced[0], sliced[1]);
|
||
|
|
|
||
|
|
print("");
|
||
|
|
print("--- Iterating Arrays ---");
|
||
|
|
colors = {"red", "green", "blue"};
|
||
|
|
print("Colors:");
|
||
|
|
for (i = 0; i < len(colors); i++) {
|
||
|
|
print(" ", colors[i]);
|
||
|
|
}
|
||
|
|
|
||
|
|
print("");
|
||
|
|
print("--- Building Arrays ---");
|
||
|
|
squares = {};
|
||
|
|
for (i = 1; i <= 5; i++) {
|
||
|
|
squares.push(i * i);
|
||
|
|
}
|
||
|
|
print("Squares of 1-5:");
|
||
|
|
for (i = 0; i < len(squares); i++) {
|
||
|
|
print(" ", squares[i]);
|
||
|
|
}
|
||
|
|
|
||
|
|
print("");
|
||
|
|
print("--- Nested Arrays ---");
|
||
|
|
matrix = {
|
||
|
|
{1, 2, 3},
|
||
|
|
{4, 5, 6},
|
||
|
|
{7, 8, 9}
|
||
|
|
};
|
||
|
|
print("3x3 Matrix:");
|
||
|
|
for (row = 0; row < len(matrix); row++) {
|
||
|
|
print(" ", matrix[row][0], matrix[row][1], matrix[row][2]);
|
||
|
|
}
|
||
|
|
|
||
|
|
print("");
|
||
|
|
print("All array tests completed");
|