An array let your store more than one piece of data into the same variable as an ordered list.
This example stores three strings into the array called names.
SET names TO ["Percy", "James", "Gordon"]
Dim names As String()
names = {"Percy", "James", "Gordon"}
names = ["Percy", "James", "Gordon"]
array names[3]
names[0] = "Percy"
names[1] = "James"
names[2] = "Gordon"
names[3]
set names[0] = "Percy"
set names[1] = "James"
set names[2] = "Gordon"
string[] names = {"Percy", "James", "Gordon"};
An array lets you store more than one piece of data into the same variable as an ordered list.
This example stores three numbers into the array called ages.
Dim ages As Integer()
ages = {14, 15, 14}
array ages[3]
ages[0] = 14
ages[1] = 15
ages[2] = 14
ages[3]
set ages[0] = 14
set ages[1] = 15
set ages[2] = 14
int[] ages = {14, 15, 14};
An array lets you store more than one piece of data into the same variable as an ordered list.
Data in an array can be accessed using an index. This is a number which defines the position in the list, which starts counting from 0
This example displays the first string in an array called names
SET names TO ["Percy", "James", "Gordon"]
SEND names[0] TO DISPLAY
Dim names As String()
names = {"Percy", "James", "Gordon"}
MsgBox(names(0))
names = ["Percy", "James", "Gordon"]
print(names[0])
array names[3]
names[0] = "Percy"
names[1] = "James"
names[2] = "Gordon"
print(names[0])
names[3]
set names[0] = "Percy"
set names[1] = "James"
set names[2] = "Gordon"
output names[0]
string[] names = {"Percy", "James", "Gordon"};
Console.WriteLine(names[0]);
If an array is like a list of data, a 2d array is like a grid of data where the data is stored in one variable but organised into rows and columns like a spreadsheet.
This example stores details of 3 different fruit for a supermarket.
It will then display the colour of the first fruit.
The first index will select the row then the second index selects the column.
SET fruit TO [["Plum", "Purple"],
["Apple", "Red"],
["Banana", "Yellow"]]
SEND fruit[0,1] TO DISPLAY
Dim fruit As String(,)
fruit = {{"Plum", "Purple"},
{"Apple", "Red"},
{"Banana", "Yellow"}}
MsgBox(fruit(0,1))
fruit = [["Plumb", "Purple"],
["Apple", "Red"],
["Banana", "Yellow"]]
print(fruit[0][1])
array fruit[3][2]
fruit[0][0] = "Plumb"
fruit[0][1] = "Purple"
fruit[1][0] = "Apple"
fruit[1][1] = "Red"
fruit[2][0] = "Banana"
fruit[2][1] = "Yellow"
print(fruit[0][1])
{ not specified in eduqas pseudocode reference }
string[,] fruit = {
{"Plumb", "Purple"},
{"Apple", "Red"},
{"Banana", "Yellow"}
};
Console.WriteLine(fruit[0,1]);
A list is similar to an array: both allow more than one piece of data to be stored in the same variable.
It's not possible to add or remove data to an array after it's been set for the first time but it is possible to add or remove data to a list.
This example creates a list called colours and adds three strings into it before displaying the first item in the list.
SET colours TO ["Red", "Green", "Blue"]
SEND colours[0] TO DISPLAY
Dim colours As New List(Of String)
colours.Add("Red")
colours.Add("Green")
colours.Add("Blue")
MsgBox(colours(0))
colours = []
colours.append("Red")
colours.append("Green")
colours.append("Blue")
print(colours[0])
array colours[3]
colours[0] = "Red"
colours[1] = "Green"
colours[2] = "Blue"
print(colours[0])
colours[3]
set colours[0] = "Red"
set colours[1] = "Green"
set colours[2] = "Blue"
output colourd[0]
// using System.Collections.Generic;
List<string> colours = new List<string>();
colours.Add("Red");
colours.Add("Green");
colours.Add("Blue");
Console.WriteLine(colours[0]);
Create a list of integers
A list is similar to an array: both allow more than one piece of data to be stored in the same variable.
It's not possible to add or remove data to an array after it's been set for the first time but it is possible to add or remove data to a list.
This example creates a list called numbers and adds three integer numbers into it before displaying the sum of all the numbers added together.
SET numbers to [1, 2, 3]
SEND SUM(numbers) TO DISPLAY
Dim numbers As New List(Of Integer)
numbers.AddRange({1, 2, 3})
MsgBox(numbers.Sum())
' You could also do:
Dim numbers As New List(Of Integer)
numbers.Add(1)
numbers.Add(2)
numbers.Add(3)
MsgBox(numbers(0) + numbers(1) + numbers(2))
numbers = []
numbers.append(1)
numbers.append(2)
numbers.append(3)
print(sum(numbers))
array numbers[3]
numbers[0] = 1
numbers[1] = 2
numbers[2] = 3
print(numbers[0] + numbers[1] + numbers[2])
numbers[3]
set numbers[0] = 1
set numbers[1] = 2
set numbers[2] = 3
output numbers[0] + numbers[1] + numbers[2]
// using System.Collections.Generic;
// using System.Linq;
List<int> numbers = new List<int>();
numbers.Add(1);
numbers.Add(2);
numbers.Add(3);
// Sum requires System.Linq
Console.WriteLine(numbers.Sum());
A list is similar to an array: both allow more than one piece of data to be stored in the same variable.
It's not possible to add or remove data to an array after it's been set for the first time but it is possible to add or remove data to a list.
This example creates a list of strings called names. It adds 3 names to the list then removes the first one.
SET names TO ["Alice", "Bob", "Charlie"]
DELETE names[0]
Dim names as New List(Of String)
names.AddRange({"Alice", "Bob", "Charlie"})
names.RemoveAt(0)
names = ["Alice", "Bob", "Charlie"]
names.pop(0)
array names[3]
names[0] = "Alice"
names[1] = "Bob"
names[2] = "Charlie"
# spec doesn't say, so I'm just guessing here...
remove names[0]
names[3]
set names[0] = "Alice"
set names[1] = "Bob"
set names[2] = "Charlie"
# spec doesn't say, so I'm just guessing here...
remove names[0]
// using System.Collections.Generic;
List<string> names = new List<string> { "Alice", "Bob", "Charlie" };
names.RemoveAt(0);
Splitting a string into an array of strings
It can be really useful to split one string into lots of smaller parts whenever you see a particular character.
This example will split the string "red,green,blue" into an array of smaller strings: "red", "green" and "blue"
It will then display the first colour: red
STRING colours
SET colours TO "red,green,blue"
parts = SPLIT(colours, ",")
SEND parts[0] TO DISPLAY
' Create a string storing comma separated values
Dim colours as String = "red,green,blue"
' Split the values into an array of strings
Dim parts() as String = colours.Split(",")
' Get the first value in the array
MsgBox(parts(0))
# Create a string storing comma separated values
colours = "red,green,blue"
# Split the values into an array of strings
parts = colours.split(",")
# Get the first value in the array
print(parts[0])
colours = "red,green,blue"
array parts = colours.split(",")
print(parts[0])
colours is string
set colours = "red,green,blue"
parts[] = colours.split(",")
output parts[0]
// Create a string storing comma separated values
string colours = "red,green,blue";
// Split the values into an array of strings
string[] parts = colours.Split(",");
// Get the first value in the array
Console.WriteLine(parts[0]);