Ahadu 2.0
አማርኛ

Collections

Holding many values under one name

So far one variable holds one value. But what if you want to keep the names of everyone in a class? You use a collection.

Open in playground This output was verified at build time
["አበበ"፣ "ከበደ"፣ "አልማዝ"]
ብዛት፦ 3

ርዝመት means "length".

Notice that a piece of text printed on its own shows no quotation marks, but inside a collection it does. That is deliberate: it is how you can tell the number 3 from the text "3" when looking at a collection.

Getting one item out

Each item has a position, and counting starts at zero.

Open in playground This output was verified at build time
አበበ
አልማዝ
አልማዝ

Starting at zero feels odd at first and stops feeling odd quickly. -1 counts back from the end.

Asking for a position that is not there is an error:

Open in playground This program fails on purpose
[AH3008] 5 ከስብስቡ ውጪ ነው። ርዝመቱ 2 ነው።: መስመር 2 ላይ: 10ኛው ካራክተር

Taking a slice

Open in playground This output was verified at build time
[20፣ 30]
[10፣ 20]
[40፣ 50]

[1:3] means "from position 1 up to but not including 3".

Changing and growing

A collection can be changed.

Open in playground This output was verified at build time
[99፣ 2፣ 3]
[99፣ 2፣ 3፣ 4]
[2፣ 3፣ 4]

አዋህድ appends and ሰርዝ removes by position.

Text, by contrast, cannot be changed in place. ስም[0] = "ለ" is an error; you build a new piece of text instead.

Walking through one

Open in playground This output was verified at build time
ድምር፦ 54
አማካይ፦ 13.5

Counting a position from zero up to the length is a shape you will write many times.

Exercise

Find the largest number in a collection.

Show the answer
Open in playground This output was verified at build time
ትልቁ፦ 45