1

In mapbasic I need to read a csv into an array but arrange each row of the csv independently.

See the current result, code etc below.

I want it to be something like a python dictionary

([Bushfire_V Char (1) ;,Bushfire_V, Bushfire Hazard - Very High,    Bushfire_Hazard,OVL2_CAT,BHR_VPI], [Agri_Ovela Char (1) ;,  Agri_Ovela, Agricultural Land Class,Agricultural_Land_Classification_Class_A_and_B, OVL_CAT,NR])

Later on I need to pass some of these to other subroutines to run a process.

For example I need

planning_tab="Bushfire_Hazard"+".tab"
planning_layer="Bushfire_V"
planning_attrib="OVL2_CAT"
planning_val="BHR_VPI"

then on the next run

planning_tab="Agricultural_Land_Classification_Class_A_and_B"+."tab"
planning_layer="Agri_Ovela"
planning_attrib="OVL_CAT"
planning_val="NR"

Input CSV enter image description here

Code base

Call ReadTextFileToArray(txtFile, myArray) '// populate array
For i = 1 to UBound(myArray)
 Print i+", "+myArray(i)
 Next    '// go to next element in array
Sub ReadTextFileToArray(ByVal txtFilePath as String, myArray() as String)

Dim i as Integer

    i = 1

    Open file txtFilePath for input as #1   '// open text file for input (to read) as #1
    Do Until EOF(1)             '// do until the end of file #1 is reached
        Redim myArray(i)        '// resize array to i
        Input #1, myArray(i)    '// read value from text file
        i = i + 1               '// increment i
    Loop                        '// return to Do Until...

End Sub

Result

1, Column
2, Cleaned
3, Friendly_Name
4, Related_Tab
5, Tab_Column
6, Value
7, Bushfire_V Char (1) ;
8, Bushfire_V
9, Bushfire Hazard - Very High
10, Bushfire_Hazard
11, OVL2_CAT
12, BHR_VPI
13, Bushfire_M Char (1) ;
14, Bushfire_M
15, Bushfire Hazard - Medium
16, Bushfire_Hazard
17, OVL2_CAT
18, BHR_MPI
19, Bushfire_H Char (1) ;
20, Bushfire_H
21, Bushfire Hazard - High
22, Bushfire_Hazard
23, OVL2_CAT
24, BHR_HPI
25, Acid_Low Char (1) ;
26, Acid_Low
27, Acid Sulfate Soil - Low Probability
28, Low_Probability_of_Acid_Sulfate_Soils
29, OVL2_CAT
30, PAS_A20
31, Acid_High Char (1) ;
32, Acid_High
33, Acid Sulfate Soil - High Probability
34, High_Probability_of_Acid_Sulfate_Soils
35, OVL2_CAT
36, PAS_A05
37, Agri_Ovela Char (1) ;
38, Agri_Ovela
39, Agricultural Land Class
40, Agricultural_Land_Classification_Class_A_and_B
41, OVL_CAT
42, NR
43, 

How do I write it into an array, tuple (or whatever Mapbasic calls it) and read from it a row at a time?

2

Why not open the CSV into a MapInfo table? Then you could easily read the data from the mapinfo table to achieve the output you want. Take a look at the Register Table command in the reference guide for details on how to do this.

  • Once I register the table as a tab file, I can't find any reference on how to read it line by line and just get the columns of a row that I need to use in the SQL queries. – GeorgeC Apr 24 '18 at 0:25
  • Accepted as I found TABLEINFO() and SEARCH_INFO_ROW – GeorgeC Apr 24 '18 at 3:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.