Skip to content
العربية

9. Structs

To gather the data of one thing in a struct.

  • A struct is a type you define yourself, made of fields: each field has a name and a type, one field per line.
  • You create a value of it by writing its name, then the values in field order: طالب("ليلى", 91).
  • ط.اسم reads the field اسم from the value ط.
Example
هيكل طالب:
اسم نص
درجة عدد64
ط = طالب("ليلى", 91)
اطبع(ط.اسم, " ", ط.درجة)
اطبع(ط)
Output recorded in the reference handbookmeem برنامج.م · 0.17.1
ليلى 91طالب { اسم: ليلى, درجة: 91 }

Try this example in the playground

The number of values does not match the number of fields:

Causes the error
هيكل نقطة:
س عدد64
ا = نقطة(1, 2)
Output recorded in the reference handbookmeem برنامج.م · 0.17.1
​[خطأ دلالي E0425] سطر 3:5 - الهيكل 'نقطة' يملك حقلاً واحداً فقط لكن مُرّر قيمتان   3 | ا = نقطة(1, 2)     |     ^~~~  تلميح: الحقول: س

Exit code: 2

In the error catalog: E0425

Try this example in the playground

Define a struct كتاب (book) with a title and a page count, make a list of three books, and print the total number of pages and the longest book.

Show the solution
Solution
هيكل كتاب:
عنوان نص
صفحات عدد64
كتب = [كتاب("الأيام", 160), كتاب("كليلة ودمنة", 290), كتاب("حي بن يقظان", 120)]
مجموع = 0
اطول = كتب[0]
لكل ك في كتب:
مجموع += ك.صفحات
اذا ك.صفحات > اطول.صفحات:
اطول = ك
اطبع("مجموع الصفحات: ", مجموع)
اطبع("أطولها: ", اطول.عنوان, " (", اطول.صفحات, " صفحة)")
حرر(كتب)
Output recorded from the compilermeem 09_حل.م · meem 0.17.3+434e13f
مجموع الصفحات: 570أطولها: كليلة ودمنة (290 صفحة)

Structs