Data Structures
Rui provides powerful data structures for organizing and manipulating data. Arrays and objects are the primary complex data types that help you build sophisticated programs.
Arrays
Arrays are ordered collections that can hold multiple values. They are perfect for storing lists of related data.
Creation and Access
Create arrays using square brackets and access elements by index:
suppose numbers = [1, 2, 3, 4, 5]
suppose fruits = ["apple", "banana", "orange"]
suppose mixed = [1, "hello", true, 3.14]
write("First number: " + numbers[0])
write("Second fruit: " + fruits[1])
write("Last number: " + numbers[-1])
write("Array length: " + length(numbers)) Array Methods
Arrays come with built-in methods for manipulation:
suppose arr = [1, 2, 3]
write("Original: " + arr)
arr.push(4, 5)
write("After push: " + arr)
suppose last = arr.pop()
write("Popped: " + last)
suppose first = arr.shift()
write("Shifted: " + first)
write("After shift: " + arr)
arr.unshift(0)
write("After unshift: " + arr) Array Slicing
Extract portions of arrays using slice notation:
suppose arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
write("First 3: " + arr[:3])
write("Last 2: " + arr[-2:])
suppose j = 0
until (j < length(fruits)) {
write("I like " + fruits[j])
j = j + 1
}Objects
Objects store data as key-value pairs, perfect for representing real-world entities.
Creation and Access
Create objects using curly braces and access properties with dot notation:
suppose person = {
name: "Alice",
age: 25,
city: "New York",
isStudent: true
}
write("Name: " + person.name)
write("Age: " + person.age)
write("City: " + person.city)
write("Is student: " + person.isStudent)
person.age = 26
person.city = "Boston"
write("Updated age: " + person.age)Nested Objects
Objects can contain other objects and arrays:
suppose company = {
name: "TechCorp",
founded: 2020,
employees: [
{
name: "Alice",
role: "Developer",
salary: 75000
},
{
name: "Bob",
role: "Designer",
salary: 65000
}
],
location: {
city: "San Francisco",
country: "USA"
}
}
write("Company: " + company.name)
write("First employee: " + company.employees[0].name)
write("Location: " + company.location.city + ", " + company.location.country)Object Methods
Objects can contain functions as properties:
suppose calculator = {
add: define(a, b) {
return a + b
},
multiply: define(a, b) {
return a * b
},
subtract: define(a, b) {
return a - b
}
}
write("5 + 3 = " + calculator.add(5, 3))
write("4 * 7 = " + calculator.multiply(4, 7))
write("10 - 2 = " + calculator.subtract(10, 2))Combining Arrays and Objects
Arrays and objects work together to create complex data structures:
suppose students = [
{
name: "Alice",
grades: [85, 92, 78, 96],
major: "Computer Science"
},
{
name: "Bob",
grades: [78, 85, 82, 88],
major: "Mathematics"
}
]
suppose i = 0
until (i < length(students)) {
suppose student = students[i]
suppose total = 0
suppose j = 0
until (j < length(student.grades)) {
total = total + student.grades[j]
j = j + 1
}
suppose average = total / length(student.grades)
write(student.name + " (" + student.major + "): " + average)
i = i + 1
}Best Practices
- Choose the right structure: Use arrays for ordered lists, objects for named properties
- Keep structures simple: Avoid overly nested structures when possible
- Use descriptive keys: Object property names should be clear and meaningful
- Initialize properly: Always provide initial values for arrays and objects
- Handle empty structures: Check for empty arrays and objects before processing
Common Patterns
Here are some common patterns for working with data structures:
suppose numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
suppose evens = []
suppose i = 0
until (i < length(numbers)) {
if (numbers[i] % 2 == 0) {
evens.push(numbers[i])
}
i = i + 1
}
write("Even numbers: " + evens)
suppose scores = [85, 92, 78, 96, 88]
suppose max = scores[0]
suppose j = 1
until (j < length(scores)) {
if (scores[j] > max) {
max = scores[j]
}
j = j + 1
}
write("Highest score: " + max)Next Steps
Now that you understand data structures, learn about String Manipulation to work with text data effectively.