Project: summarising a CSV file
The idea: مبيعات <ملف.csv> totals, for each region, the number of lines, the quantity and the revenue, and finds the best month. Bad lines are counted and do not stop the program.
The data
Section titled “The data”The file has two deliberately bad lines: a line with missing fields (9), and a line whose quantity is a word, not a number (10).
التاريخ,المنطقة,المنتج,الكمية,السعر 2026-01-05,الرياض,قلم,10,2.5 2026-01-07,القاهرة,دفتر,4,7.25 2026-02-11,الرياض,دفتر,3,7.25 2026-02-14,تونس,قلم,20,2.5 2026-03-02,القاهرة,قلم,8,2.5 2026-03-09,تونس,حقيبة,1,40.0 2026-03-12,الرياض,حقيبة,2,40.0 2026-03-15,القاهرة,ممحاة 2026-04-01,تونس,قلم,خمسة,2.5 2026-04-03,الرياض,قلم,6,2.5
The program
Section titled “The program”# معالجة ملف CSV: إجمالي المبيعات لكل منطقة ولكل شهر، مع عدّ الأسطر الفاسدةاستورد نظاماستورد ملفاتاستورد رياضيات
هيكل اجمالي: منطقة نص كمية عدد64 ايراد كسر64 اسطر عدد64
هيكل سجل: شهر عدد64 منطقة نص كمية عدد64 سعر كسر64
اخطاء: حقول_ناقصة
داله حلل_سطر(سطر نص) !سجل: ح = تقسيم(سطر, ",") خاتمه حرر(ح) اذا طول(ح) != 5: ارجع خطأ.حقول_ناقصة تاريخ = تقسيم(ح[0], "-") خاتمه حرر(تاريخ) اذا طول(تاريخ) != 3: ارجع خطأ.صيغة_خاطئة شهر = جرّب حلل(تاريخ[1], عدد64) كمية = جرّب حلل(ح[3], عدد64) سعر = جرّب حلل(ح[4], كسر64) ارجع سجل(شهر, ح[1], كمية, سعر)
داله قبل(ا اجمالي, ب اجمالي) منطقي: ارجع ا.ايراد > ب.ايراد
داله رئيسي() !فراغ: وسائط = نظام.وسائط() اذا طول(وسائط) != 1: اطبع("الاستعمال: مبيعات <ملف.csv>") نظام.انهِ(2) سطور = جرّب ملفات.اقرأ_سطور(وسائط[0]) خاتمه حرر(سطور) مناطق قاموس[نص, عدد64] = {} # المنطقة ← موضعها في القائمة خاتمه حرر(مناطق) جدول قائمة[اجمالي] = [] خاتمه حرر(جدول) اشهر مصفوفة[كسر64, 13] = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] فاسدة = 0 اول_فاسد = "" لكل ع في مدى(1, طول(سطور)): اذا طول(سطور[ع]) == 0: تجاوز س = حلل_سطر(سطور[ع]) والا خ: فاسدة += 1 اذا فاسدة == 1: اول_فاسد = دمج_نصوص("سطر ", ع + 1, ": ", خ) تجاوز اذا !يوجد(مناطق, س.منطقة): مناطق[س.منطقة] = طول(جدول) اضف(جدول, اجمالي(س.منطقة, 0, 0.0, 0)) م = مناطق[س.منطقة] ق = جدول[م] ق.كمية += س.كمية ق.ايراد += كسر64(س.كمية) * س.سعر ق.اسطر += 1 جدول[م] = ق اشهر[س.شهر] += كسر64(س.كمية) * س.سعر رتب_ب(جدول, قبل) اطبع("المنطقة الأسطر الكمية الإيراد") كلي = 0.0 لكل ق في جدول: اطبع(ق.منطقة, "\t", رياضيات.نسق_عدد(ق.اسطر, 8, خطاء), رياضيات.نسق_عدد(ق.كمية, 10, خطاء), " ", رياضيات.نسق(ق.ايراد, 2)) كلي += ق.ايراد اطبع("الإجمالي: ", رياضيات.نسق(كلي, 2), " أسطر فاسدة: ", فاسدة, " (", اول_فاسد, ")") افضل = 1 لكل ش في مدى(2, 13): اذا اشهر[ش] > اشهر[افضل]: افضل = ش اطبع("أفضل شهر: ", افضل, " بإيراد ", رياضيات.نسق(اشهر[افضل], 2))
جرّب رئيسي()Also built with meem build when the site was built: a static file of 90.5 kB, run on its own, and its output matched the output above.
Step by step
Section titled “Step by step”- Parsing a line:
حلل_سطر(parse line) returns!سجل(it used to be calledحلل, which became the name of a builtin in 0.12.0). It splits the line withتقسيم(سطر, ","), and returns its errorحقول_ناقصة(missing fields) if there are not five fields. Converting the numbers withجرّب حلل(الحقل, عدد64)(andكسر64for the price) passes onصيغة_خاطئة(wrong format) if a field is not a number. - A bad line does not stop the program:
حلل(سطر) والا خ:counts the bad line, keeps a description of the first one, thenتجاوز(continue) moves to the next line. - Totalling: the dictionary
مناطقkeeps each region’s position in the listجدول, and a fixed arrayاشهرof 13 slots totals the revenue for each month. - Sorting and printing:
رتب_بby revenue, andرياضيات.نسقto two decimal places.
What was hard
Section titled “What was hard”- Changing an element of a list: the report’s author wrote
ق = جدول[م]thenجدول[م] = ق, thinking that changing a field of a list element was not possible. Then they found thatجدول[م].كمية += س.كميةworks directly. What was missing was in the documentation, not in the language. - The parts of
تقسيم:خاتمه حرر(ح)gives back the list alone, and the strings of its parts stay. The compiler does not mention this by default; with--warn-unprovenit warns with E0473 on the twoتقسيمlines, and its hint is to build them in an arena and give it back after the line. - Memory and time: on 200 thousand lines: 0.64 s built statically (0.48 dynamically) and 76.1 MB, against 0.54 s and 10.9 MB for Python.
What we learned
Section titled “What we learned”والا خ:withتجاوزmakes «a bad line» an ordinary case that is counted, not an exception that stops everything.- A fixed array (
مصفوفة[كسر64, 13]) suits a known number of slots, and allocates nothing from the heap.