Skip to content
العربية

7. Dictionaries

To map a key to a value, and to go through the keys in the order they were inserted.

  • A dictionary maps each key to a value: {"سارة": 30}. You reach the value through its key: اعمار["سارة"].
  • Assigning a value to a new key adds it to the dictionary.
  • لكل اسم في اعمار: goes through the keys in insertion order. مفاتيح(ق) (keys) returns them as a list in the same order.
Example
اعمار = {"سارة": 30, "علي": 25}
اعمار["منى"] = 28
لكل اسم في اعمار:
اطبع(اسم, ": ", اعمار[اسم])
اطبع(مفاتيح(اعمار))
Output recorded in the reference handbookmeem برنامج.م · 0.17.1
سارة: 30علي: 25منى: 28[سارة, علي, منى]

Try this example in the playground

Reading a key that does not exist is a runtime error. Check it with يحوي (contains) first:

Causes the error
ع = {"سارة": 30}
اطبع(ع["علي"])
Output recorded in the reference handbookmeem برنامج.م · 0.17.1
[خطأ تشغيل E0337] سطر 2:7 - المفتاح 'علي' غير موجود في القاموس  اطبع(ع["علي"])

Exit code: 3

In the error catalog: E0337

Try this example in the playground

Fixes it
ع = {"سارة": 30}
اذا يحوي(ع, "علي"):
اطبع(ع["علي"])
غير ذلك:
اطبع("لا عمر لعلي")
Output recorded in the reference handbookmeem برنامج.م · 0.17.1
لا عمر لعلي

Try this example in the playground

Count how often each letter appears in «مرحبا بالعالم», ignoring the space, and print the count for each letter in the order it first appears.

Show the solution
Solution
كلمة = "مرحبا بالعالم"
عداد قاموس[رمز, عدد64] = {}
لكل ح في كلمة:
اذا ح == ' ':
تجاوز
اذا يوجد(عداد, ح):
عداد[ح] = عداد[ح] + 1
غير ذلك:
عداد[ح] = 1
لكل ح في عداد:
اطبع(ح, ": ", عداد[ح])
حرر(عداد)
Output recorded from the compilermeem 07_حل.م · meem 0.17.3+434e13f
م: 2ر: 1ح: 1ب: 2ا: 3ل: 2ع: 1

Types · Builtins: مفاتيح and يحوي