Skip to content
العربية

6. Lists

To add to a list, sort it, and search it.

  • A list holds elements of one type between square brackets, and grows as you add to it: [70, 95, 82].
  • اضف(ق, قيمة) (add) appends the value at the end. رتب(ق) (sort) sorts it ascending in place. طول(ق) (length) is the number of its elements.
  • بحث(ق, قيمة) (search) returns the index of the first element equal to the value, or لاشيء (nothing) if it is not found (its type is عدد64؟), so write بحث(ق, قيمة) والا -1 if you always want a number. The index is the element’s position number, starting from 0.
  • A list is a reference: after ب = ق, ب and ق are two names for the same list, and nothing is copied.
Example
درجات = [70, 95, 82]
اضف(درجات, 60)
رتب(درجات)
اطبع(درجات, " ", طول(درجات), " ", بحث(درجات, 95))
Output recorded in the reference handbookmeem برنامج.م · 0.17.1
[60, 70, 82, 95] 4 3

Try this example in the playground

An index outside the list:

Causes the error
ق = [1, 2, 3]
اطبع(ق[3])
Output recorded in the reference handbookmeem برنامج.م · 0.17.1
[خطأ تشغيل E0321] سطر 2:7 - الفهرس 3 خارج الحدود (الحجم 3)  اطبع(ق[3])

Exit code: 3

In the error catalog: E0321

Try this example in the playground

Fixes it
ق = [1, 2, 3]
اطبع(ق[طول(ق) - 1])
Output recorded in the reference handbookmeem برنامج.م · 0.17.1
3

Try this example in the playground

Another mistake is thinking that ب = ق copies the list: it does not, because a list is a reference.

In the list [70, 95, 82, 64, 95], find the highest grade with a loop (without رتب), then its first position with بحث.

Show the solution
Solution
درجات = [70, 95, 82, 64, 95]
اكبر = درجات[0]
لكل د في درجات:
اذا د > اكبر:
اكبر = د
اطبع("أكبر درجة: ", اكبر)
اطبع("موضعها الأول: ", بحث(درجات, اكبر))
حرر(درجات)
Output recorded from the compilermeem 06_حل.م · meem 0.17.3+434e13f
أكبر درجة: 95موضعها الأول: 1

Types · Builtins