### Author Topic: Dynamic arrays  (Read 221 times)

#### jj2007

• Full Member
• Posts: 170
• MasmBasic rocks
##### Dynamic arrays
« on: 18. July 2019, 00:40:00 »
This example reads a text file that has the format Country   Series    Value1  Value 2 into a numeric matrix, i.e. a two-dimensional numeric array - useful for working with spreadsheets and tables. Project attached, MasmBasic of 16 July 2019 or later is required.

include \masm32\MasmBasic\MasmBasic.inc
Init                                ; *** read values from a text file into a numeric array ***
Recall "UnderFiveMortalityRate.tab", L\$(), tab        ; tab-delimited text -> two-dimensional string array
Clr ecx, edi                                          ; we need two counters
Print "Under 5 mortality rate", CrLf\$, L\$(0, 0)       ; Country
Print At(22) "  ", L\$(0, 2), Tb\$, L\$(0, 3)            ; 1990, 2013
QSort L\$(), 0, 2003h                ; sort string array numerically by column 3
Delete L\$(0)                        ; we don't want the captions
Dim Under5() As double              ; create a dynamic numeric array
Dim StringIndex() As DWORD          ; and another one to keep track of the names
.Repeat
xor esi, esi                  ; column counter
.Repeat
MovVal <Under5(edi, esi)>, L\$(ecx, esi+2)    ; assign values to the numeric array
inc esi
.Until edx==-127 || esi>=99     ; MovVal returns -127 in edx if no valid number was found
.if esi>2
mov StringIndex(edi), ecx       ; remember the country
inc edi         ; advance numeric array row index only if at least two values found
.endif
inc ecx
.Until ecx>=L\$(?)             ; loop until strings are finished
For_ ct=0 To Under5(?rows)-1
Print Str\$("\n%i\t", ct), L\$(StringIndex(ct), 0)
Print At(22) " "
For_ ecx=0 To Under5(?cols)-1
Print Tb\$, Str\$("%4f", Under5(ct, ecx))
Next
Next
PrintLine CrLf\$
deb 4, "total strings", L\$(?)
deb 4, "total numbers", Under5(?)
deb 4, "valid rows", Under5(?rows)
deb 4, "valid columns", Under5(?cols)
Inkey "--- hit any key ---"
EndOfCode

Output:
Code: [Select]
`Under 5 mortality rateCountry                 1990    20130       Iceland         6.400   2.1001       Finland         6.700   2.6002       Singapore       7.700   2.8003       Sweden          6.900   3.0004       Italy           9.600   3.6005       Austria         9.500   3.9006       Germany         8.500   3.9007       Australia       9.200   4.0008       France          9.000   4.2009       Switzerland     8.200   4.20010      Belgium         10.000  4.40011      United Kingdom  9.300   4.60012      Cuba            13.30   6.20013      United States   11.20   6.90014      Yemen           124.8   51.3015      Zambia          192.5   87.4016      Zimbabwe        74.60   88.5017      Afghanistan     179.1   97.30total strings   eaX             23total numbers   eax             36valid rows      eax             18valid columns   eax             2`