Arrays in SAPL

Learn how to store and manipulate collections of data using arrays

Learning Objectives

  • Understand what arrays are and why they're important in programming
  • Learn how to create, access, and modify arrays in SAPL
  • Master common array operations like adding and removing elements
  • Explore techniques for iterating through arrays
  • Apply arrays to solve practical programming problems

Introduction to Arrays

Arrays (also called lists in some programming languages) are ordered collections of items. They allow you to store multiple values in a single variable, making it easier to manage and manipulate related data.

Think of an array as a container with numbered compartments, each holding a value. These compartments are called elements, and each element has an index that identifies its position in the array. In SAPL, as in most programming languages, array indices start at 0.

Arrays are incredibly versatile and are used in virtually every non-trivial program. They can store any type of data, including numbers, strings, booleans, and even other arrays.

Creating Arrays

In SAPL, arrays are created using square brackets [] with comma-separated values.

Basic Array Creation

Here's how to create arrays with different types of elements:

# የቁጥሮች **ድርድር** 
ቁጥሮች = [1, 2, 3, 4, 5]
አሳይ(ቁጥሮች)

# የሕብረቁምፊዎች  **ድርድር**
ስሞች = ["አበበ", "ከበደ", "አልማዝ", "ፍቅርተ"]
አሳይ(ስሞች)

# የቦሊያን  **ድርድር**
ሁኔታዎች = [እውነት, ሀሰት, እውነት, ሀሰት]
አሳይ(ሁኔታዎች)

# የተቀላቀለ **ድርድር** (የተለያዩ አይነቶች)
ድብልቅ = [1, "ሰላም", እውነት, 3.14]
አሳይ(ድብልቅ)
SAPL

Output:

[1, 2, 3, 4, 5] ["አበበ", "ከበደ", "አልማዝ", "ፍቅርተ"] [እውነት, ሀሰት, እውነት, ሀሰት] [1, "ሰላም", እውነት, 3.14]

Empty and Nested Arrays

You can create empty arrays and arrays that contain other arrays (nested arrays):

# ባዶ **ድርድር** 
ባዶ_ድርድር = []
አሳይ(ባዶ_ድርድር)

# **ድርድር_ውስጥ_ድርድር** (የተለያዩ ድርድሮች)
ድርድር_ውስጥ_ድርድር = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
አሳይ(ድርድር_ውስጥ_ድርድር)
SAPL

Output:

[] [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

Accessing Array Elements

You can access individual elements in an array using square brackets with an index. Remember that array indices start at 0, so the first element is at index 0, the second at index 1, and so on.

Accessing by Index

Here's how to access array elements by their index:

# **ድርድር**  **ፍጠር**
ፍራፍሬዎች = ["ሙዝ", "አፕል", "ብርቱካን", "ፓፓያ", "ማንጎ"]

# **ኤለመንቶችን**  **በኢንዴክስ**  **አግኝ**
አሳይ("የመጀመሪያው ፍራፍሬ: " + ፍራፍሬዎች[0])  # ሙዝ
አሳይ("ሁለተኛው ፍራፍሬ: " + ፍራፍሬዎች[1])  # አፕል
አሳይ("ሶስተኛው ፍራፍሬ: " + ፍራፍሬዎች[2])  # ብርቱካን

# **የመጨረሻውን ኤለመንት**  **አግኝ**
አሳይ("የመጨረሻው ፍራፍሬ: " + ፍራፍሬዎች[4])  # ማንጎ

# **አሉታዊ ኢንዴክሶች**  **ከመጨረሻው ይቆጥራሉ**
አሳይ("የመጨረሻው ፍራፍሬ (ከኋላ): " + ፍራፍሬዎች[-1])  # ማንጎ
አሳይ("ከመጨረሻው ሁለተኛው ፍራፍሬ: " + ፍራፍሬዎች[-2])  # ፓፓያ
SAPL

Output:

የመጀመሪያው ፍራፍሬ: ሙዝ ሁለተኛው ፍራፍሬ: አፕል ሶስተኛው ፍራፍሬ: ብርቱካን የመጨረሻው ፍራፍሬ: ማንጎ የመጨረሻው ፍራፍሬ (ከኋላ): ማንጎ ከመጨረሻው ሁለተኛው ፍራፍሬ: ፓፓያ

Accessing Elements in Nested Arrays

For nested arrays, you can use multiple indices to access elements at different levels:

# **ድርድር_ውስጥ_ድርድር** 
ድርድር_ውስጥ_ድርድር = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

# **ኤለመንቶችን**  **በድርድር_ውስጥ_ድርድር**  **ውስጥ አግኝ**
አሳይ("የመጀመሪያው ድርድር: " ,ድርድር_ውስጥ_ድርድር[0])  # [1, 2, 3]
አሳይ("የመጀመሪያው ድርድር ውስጥ የመጀመሪያው ቁጥር: " ,ድርድር_ውስጥ_ድርድር[0][0])  # 1
አሳይ("ሁለተኛው ድርድር ውስጥ ሶስተኛው ቁጥር: " ,ድርድር_ውስጥ_ድርድር[1][2])  # 6
SAPL

Output:

የመጀመሪያው ድርድር: [1, 2, 3] የመጀመሪያው ድርድር ውስጥ የመጀመሪያው ቁጥር: 1 ሁለተኛው ድርድር ውስጥ ሶስተኛው ቁጥር: 6

Modifying Arrays

Arrays in SAPL are mutable, which means you can change their elements after they're created.

Changing Elements

You can change the value of an array element by assigning a new value to it:

# **ድርድር**  **ፍጠር**
ፍራፍሬዎች = ["ሙዝ", "አፕል", "ብርቱካን", "ፓፓያ", "ማንጎ"]
አሳይ("ቀድሞ: " + ፍራፍሬዎች)

# **ኤለመንት**  **አስጨምር**
ፍራፍሬዎች[1] = "ፖም"
አሳይ("ከለወጥን በኋላ: " + ፍራፍሬዎች)
SAPL

Output:

ቀድሞ: ["ሙዝ", "አፕል", "ብርቱካን", "ፓፓያ", "ማንጎ"] ከለወጥን በኋላ: ["ሙዝ", "ፖም", "ብርቱካን", "ፓፓያ", "ማንጎ"]

Adding Elements

You can add elements to an array using the አስጨምር function (similar to "append" in other languages):

# **ድርድር**  **ፍጠር**
ፍራፍሬዎች = ["ሙዝ", "አፕል", "ብርቱካን"]
አሳይ("ቀድሞ: " + ፍራፍሬዎች)

# **አስጨምር**  **በመጨረሻው ይጨምራሉ**
አስጨምር(ፍራፍሬዎች, "ፓፓያ")
አሳይ("አንድ ከጨመርን በኋላ: " + ፍራፍሬዎች)

አስጨምር(ፍራፍሬዎች, "ማንጎ")
አሳይ("ሁለት ከጨመርን በኋላ: " + ፍራፍሬዎች)
SAPL

Output:

ቀድሞ: ["ሙዝ", "አፕል", "ብርቱካን"] አንድ ከጨመርን በኋላ: ["ሙዝ", "አፕል", "ብር���ካን", "ፓፓያ"] ሁለት ከጨመርን በኋላ: ["ሙዝ", "አፕል", "ብርቱካን", "ፓፓያ", "ማንጎ"]

Removing Elements

You can remove elements from an array using the አጥፋ function (similar to "pop" in other languages), which removes and returns the last element:

# **ድርድር**  **ፍጠር**
ፍራፍሬዎች = ["ሙዝ", "አፕል", "ብርቱካን", "ፓፓያ", "ማንጎ"]
አሳይ("ቀድሞ: " + ፍራፍሬዎች)

# **አጥፋ**  **በመጨረሻው ይጨምራሉ**
የተወገደው = አጥፋ(ፍራፍሬዎች)
አሳይ("የተወገደው ፍራፍሬ: " + የተወገደው)
አሳይ("ከወገድን በኋላ: " + ፍራፍሬዎች)

# **አጥፋ**  **በመጨረሻው ይጨምራሉ**
የተወገደው = አጥፋ(ፍራፍሬዎች)
አሳይ("የተወገደው ፍራፍሬ: " + የተወገደው)
አሳይ("ከወገድን በኋላ: " + ፍራፍሬዎች)
SAPL

Output:

ቀድሞ: ["ሙዝ", "አፕል", "ብርቱካን", "ፓፓያ", "ማንጎ"] የተወገደው ፍራፍሬ: ማንጎ ከወገድን በኋላ: ["ሙዝ", "አፕል", "ብርቱካን", "ፓፓያ"] የተወገደው ፍራፍሬ: ፓፓያ ከወገድን በኋላ: ["ሙዝ", "አፕል", "ብርቱካን"]

Common Array Operations

SAPL provides several built-in functions and operations for working with arrays.

Array Length

You can get the length (number of elements) of an array using the ብዛት function:

# **ድርድር**  **ፍጠር**
ፍራፍሬዎች = ["ሙዝ", "አፕል", "ብርቱካን"]
አትክልቶች = ["ካሮት", "ቲማቲም", "ኪያር"]

# **ብዛት**  **በድርድር ውስጥ ይጠቀሙ**
አሳይ("የፍራፍሬዎች ብዛት: " + ብዛት(ፍራፍሬዎች))
አሳይ("የአትክልቶች ብዛት: " + ብዛት(አትክልቶች))

# **ብዛት**  **በሰላምታ ውስጥ ይጠቀሙ**
ሰላምታ = "ሰላም አለም!"
አሳይ("የሰላምታ ርዝመት: " + ብዛት(ሰላምታ))
SAPL

Output:

የፍራፍሬዎች ብዛት: 3 የአትክልቶች ብዛት: 3 የሰላምታ ርዝመት: 12

Checking for Membership

You can check if an element is present in an array using the ውስጥ operator:

# **ድርድር**  **ፍጠር**
ፍራፍሬዎች = ["ሙዝ", "አፕል", "ብርቱካን"]

# **ውስጥ**  **በፍራፍሬዎች ውስጥ አለ?**
አሳይ("ሙዝ በፍራፍሬዎች ውስጥ አለ? " + ("ሙዝ" ውስጥ ፍራፍሬዎች))
አሳይ("ፒር በፍራፍሬዎች ውስጥ አለ? " + ("ፒር" ውስጥ ፍራፍሬዎች))
SAPL

Output:

ሙዝ በፍራፍሬዎች ውስጥ አለ? እውነት ፒር በፍራፍሬዎች ውስጥ አለ? ሀሰት

Concatenating Arrays

You can concatenate (join) two arrays using the + operator:

# **ድርድር**  **ፍጠር**
ፍራፍሬዎች = ["ሙዝ", "አፕል", "ብርቱካን"]
አትክልቶች = ["ካሮት", "ቲማቲም", "ኪያር"]

# **ምግቦች**  **በድርድር ውስጥ ይጨምራሉ**
ምግቦች = ፍራፍሬዎች + አትክልቶች
አሳይ("የተጣመሩ ምግቦች: " + ምግቦች)
SAPL

Output:

የተጣመሩ ምግቦች: ["ሙዝ", "አፕል", "ብርቱካን", "ካሮት", "ቲማቲም", "ኪያር"]

Looping Through Arrays

You can use loops to iterate through the elements of an array and perform operations on each element.

For Loops

The most common way to loop through an array is using a loop:

# **ድርድር**  **ፍጠር**
ፍራፍሬዎች = ["ሙዝ", "አፕል", "ብርቱካን"]

# **ለ**  **በፍራፍሬዎች ውስጥ ይለሙ**
ለ ፍራፍሬ በ ፍራፍሬዎች:
    አሳይ("ፍራፍሬ: " + ፍራፍሬ)
SAPL

Output:

ፍራፍሬ: ሙዝ ፍራፍሬ: አፕል ፍራፍሬ: ብርቱካን

While Loops

You can also use a እስከ loop to iterate through an array using indices:

# **ድርድር**  **ፍጠር**
ፍራፍሬዎች = ["ሙዝ", "አፕል", "ብርቱካን"]

# **ለ**  **በፍራፍሬዎች ውስጥ ይለሙ**
i = 0
እስከ i < ብዛት(ፍራፍሬዎች):
    አሳይ("ፍራፍሬ በ " + i + " ላይ: " + ፍራፍሬዎች[i])
    i = i + 1
SAPL

Output:

ፍራፍሬ በ 0 ላይ: ሙዝ ፍራፍሬ በ 1 ላይ: አፕል ፍራፍሬ በ 2 ላይ: ብርቱካን

Summary

Arrays are fundamental data structures in SAPL that allow you to store and manipulate collections of data. You've learned how to create arrays, access and modify their elements, perform common array operations, and loop through arrays to process their contents.

With this knowledge, you can now use arrays to solve a wide range of programming problems, from managing lists of items to processing complex data sets.