Accessing an array element by value versus index?

I’m currently using the following code in xojo to populate an array usingg elements from a CSV file, and later assign the results to variables. It works pretty well.

For row as integer = 0 to UBound(CSV_Array)
dim rowData as string = CSV_Array(row)
CSV_Line_Array() = Split(rowData, “,”)

i_macAddress = CSV_Line_Array(0)
i_userId = CSV_Line_Array(1)
i_firstName = CSV_Line_Array(2)
i_lastName = CSV_Line_Array(3)

Next row

What I’d like to do is, rather than using the index number for an array element, is to use the values of the column. Something like this (obviously this code doesn’t work)

i_macAddress = CSV_Line_Array("#MACADDRESS")
i_userId = CSV_Line_Array("#USERID")
i_firstName = CSV_Line_Array("#FIRSTNAME")
i_lastName = CSV_Line_Array("#LASTNAME")

The reason I want to do this is so that when I read in the CSV file, it no longer will matter what order the columns are in, and instead of column position, it relies on the header value. I’m not sure how to get here. Any suggestions?

SQLITE DATABASE solves all those problems and many more

I don’t see how that solves my core goal… which is allowing the import file columns to be in any order the user presents them in. Storing the data is trivial, it’s accessing it by column header value prior to normalization and storing that I’m trying to achieve.

Use an enum. You can convert to an index with Integer( enumValue ) and it’s easy to manage.

Use the header data to determine the SQLite field names, and order becomes unimportant. SQL cares not what order the data came in, nor the order in which it goes out.

Kem… an Enum would require the order of fields be known ahead of time…which it seems is the issue… the any input file could contain the fields in any order left to right, assuming I am understanding the “core goal” here

Ah yes, I did miss that.

or course a DICTIONARY instead of ENUM might work

dict.value("MACADDRESS")=<the column position>
i_macAddress = CSV_Line_Array(dict.value("MACADDRESS"))

of course this example does NOT include what it would take to build said dictionary

How about this… if you know that the header names themselves don’t change, read line zero first and put the values into an array.

headers = split(RowData, “,”)

Then when you want the column, use indexOf:

i_macAddress = CSV_Line_Array(Headers.IndexOf(“#macaddress”))

Greg, this might be just what I’m looking for. Let me try that out.

So my final code is this… Working great (I run this before the line by line parsing of each line in the CSV file, so this just focuses on the header row)

[code]For row as integer = 0 to 0

dim rowData as string = CSV_Array(row)
dim header() as string
header = Split(rowData, “,”)

i_altExt = CSV_Line_Array(header.IndexOf("#altExt"))

next row[/code]

That’s exactly what I was looking for! Thanks so much!