Page 1 of 1

Dealing with an unknown number of levels in an array

Posted: Tue Sep 22, 2015 5:04 pm
by pink
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:

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
can anyone think of a better way to handle the multiple levels that someone might use?

Re: Dealing with an unknown number of levels in an array

Posted: Tue Sep 22, 2015 6:28 pm
by sritcp
The following seems to work. I tried it in a button, but it could be made into a function.

Code: Select all

on mouseUp
   local tString, tKey, tArray
   put field "Field1" into tString -- Field1 contains "address:home:postal"
   set itemDel to colon
   repeat for each item tItem in tString
      put "[" & quote & tItem & quote & "]" after tKey
   end repeat
   do "put 98765 into" && "tArray" & tKey -- replace 98765 with your value
   put tArray["address"]["home"]["postal"] into msg -- check to see if it works; it does!
end mouseUp
Regards,
Sri

Re: Dealing with an unknown number of levels in an array

Posted: Tue Sep 22, 2015 7:05 pm
by bn
Hi pink,

a very nifty trick I learned from Marc Wieder:

Code: Select all

on mouseUp
   put "address:work:street" into t1
   split t1 by ":"
   put "address:work:city" into t2
   split t2 by ":"
   put "name:first" into t3
   split t3 by ":"
   put "1" into tArray [t1]
   put "2" into tArray [t2]
   put "3" into tArray [t3]
end mouseUp
look at this in the debugger to see tArray

Kind regards

Bernd

Re: Dealing with an unknown number of levels in an array

Posted: Tue Sep 22, 2015 7:55 pm
by pink
bn wrote:Hi pink,

a very nifty trick I learned from Marc Wieder:

Code: Select all

on mouseUp
   put "address:work:street" into t1
   split t1 by ":"
   put "address:work:city" into t2
   split t2 by ":"
   put "name:first" into t3
   split t3 by ":"
   put "1" into tArray [t1]
   put "2" into tArray [t2]
   put "3" into tArray [t3]
end mouseUp
look at this in the debugger to see tArray

Kind regards

Bernd
That is a very neat trick, and definitely better than what I was working with.

Thanks!