Arrays and Lists

Although some output languages don't consider there to be a difference between arrays and lists, GLS defines them as:

  • Array: A fixed length data structure of a single templated type
  • List: A variable length data structure of a single templated type

GLS considers the two to be two different data structures and has mostly separate commands for each.

Arrays

Because arrays are fixed-length, there are very few operations available on them.

Create new arrays with array new, which takes in the type of array and any number of initial items in the array. For variables, declare the type of the array with array type, which takes in the type of the array.

Retrieve a single member of an array with array index, which takes in a name of a container and an integer index.

array index : container 1
  • In C#: container[1]
  • In Python: container[1]

Get the length of an array with array length, which takes in a name of an array.

variable : fruits { array type : string } { array new : string "apple" "banana" "cherry" }

print : { string format : ("There are {0} fruits.") { array length : fruits } int }
print : { string format : ("The first fruit is {0}.") { array index : fruits 0 } string }

In C#:

string[] fruits = new string[] { "apple", "banana", "cherry" };

Console.WriteLine(string.Format("There are {0} fruits.", fruits.Length));
Console.WriteLine(string.Format("The first fruit is {0}.", fruits[0]));

In Python:

fruits = ["apple", "banana", "cherry"]

print("There are {0} fruits.".format(len(fruits)))
print("The first fruit is {0}.".format(fruits[0]))

Lists

GLS lists are much more flexible than arrays. They can be dynamically resized, added onto one another, and sort (for primitive types).

Retrieve a single member of a list with list index, which takes in a name of a container and an integer index.

list index : container 1
  • In C#: container[1]
  • In Python: container[1]

Similar to arrays, create a new list with list new, declare a list type with list type, and get a list's length with list length. Add a single item to a list with list pop, which takes in a name of a list and a new item, or add a full list to another list with list add list, which takes in the name of an existing list and a second list to add to the existing list.

variable : fruits { list type : string } { list new : string "apple" "banana" "cherry" }

list push : fruits "dragonberry"
list add list : fruits { list new : string "elderberry" "fig" }

print : { string format : ("There are {0} fruits.") { list length : fruits } int }
print : { string format : ("The first fruit is {0}.") { list index : fruits 0 } string }
print : { string format : ("The last fruit is {0}.") { list index : fruits { operation : { list length : fruits } minus 1 } } string }

In C#:

using System;

List<string> fruits = new List<string> { "apple", "banana", "cherry" };

fruits.Add("dragonberry");
fruits.AddRange(new List<string> { "elderberry", "fig" });

Console.WriteLine(string.Format("There are {0} fruits.", fruits.Count));
Console.WriteLine(string.Format("The first fruit is {0}.", fruits[0]));
Console.WriteLine(string.Format("The last fruit is {0}.", fruits[fruits.Count - 1]));

In Python:

fruits = ["apple", "banana", "cherry"]

fruits.append("dragonberry")
fruits.extend(["elderberry", "fig"])

print("There are {0} fruits.".format(fruits.len()))
print("The first fruit is {0}.".format(fruits[0]))
print("The last fruit is {0}.".format(fruits[len(fruits) - 1]))

results matching ""

    No results matching ""