Author Topic: Dynamic arrays  (Read 1212 times)

jj2007

  • Guest
Dynamic arrays
« on: July 17, 2019, 10:40:00 PM »
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 rate
Country                 1990    2013
0       Iceland         6.400   2.100
1       Finland         6.700   2.600
2       Singapore       7.700   2.800
3       Sweden          6.900   3.000
4       Italy           9.600   3.600
5       Austria         9.500   3.900
6       Germany         8.500   3.900
7       Australia       9.200   4.000
8       France          9.000   4.200
9       Switzerland     8.200   4.200
10      Belgium         10.000  4.400
11      United Kingdom  9.300   4.600
12      Cuba            13.30   6.200
13      United States   11.20   6.900
14      Yemen           124.8   51.30
15      Zambia          192.5   87.40
16      Zimbabwe        74.60   88.50
17      Afghanistan     179.1   97.30

total strings   eaX             23
total numbers   eax             36
valid rows      eax             18
valid columns   eax             2