Dealing with an unknown number of levels in an array
Posted: Tue Sep 22, 2015 5:04 pm
I am working on a program to convert a table in a SQL database to a CouchDB database.
In the simplest terms, the program will list out the fields and the user specifies the field names they map to. Once the process begins, the data from the table will be put into an array.
Here's an example of what the mapping might look like:
FROM_SQL TO_COUCHDB
gender gender
age age
firstname name:first
lastname name:last
homestreet address:home:street
homecity address:home:city
homepostal address:home:postal
workstreet address:work:street
workcity address:work:city
workpostal address:work:postal
In this example, firstname and lastname are nested under name in the array.
Address fields are nested two levels deep.
so ultimately I need to put "workpostal" into tArray["address"]["work"]["postal"]
My dilemma is that I don't know how many levels deep the user will end up going.
Currently I'm using something like this:
can anyone think of a better way to handle the multiple levels that someone might use?
In the simplest terms, the program will list out the fields and the user specifies the field names they map to. Once the process begins, the data from the table will be put into an array.
Here's an example of what the mapping might look like:
FROM_SQL TO_COUCHDB
gender gender
age age
firstname name:first
lastname name:last
homestreet address:home:street
homecity address:home:city
homepostal address:home:postal
workstreet address:work:street
workcity address:work:city
workpostal address:work:postal
In this example, firstname and lastname are nested under name in the array.
Address fields are nested two levels deep.
so ultimately I need to put "workpostal" into tArray["address"]["work"]["postal"]
My dilemma is that I don't know how many levels deep the user will end up going.
Currently I'm using something like this:
Code: Select all
put field "toCouch3" into temp3 ---this would be something like "address:work:postal"
set itemdel to colon
put the number of items in temp3 into tNum
switch tNum
case "1"
put tValue into tArray[item 1 of temp3]
break
case "2"
put tValue into tArray[item 1 of temp3][item 2 of temp3]
break
case "3"
put tValue into tArray[item 1 of temp3][item 2 of temp3][item 3 of temp3]
break
case "4"
put tValue into tArray[item 1 of temp3][item 2 of temp3][item 3 of temp3][item 4 of temp3]
break
case "5"
put tValue into tArray[item 1 of temp3][item 2 of temp3][item 3 of temp3][item 4 of temp3][item 5 of temp3]
break
end switch