Dealing with an unknown number of levels in an array

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
pink
Posts: 280
Joined: Wed Mar 12, 2014 6:18 pm

Dealing with an unknown number of levels in an array

Post by pink » 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:

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?
Greg (pink) Miller

MadPink, LLC
I'm Mad, Pink and Dangerous to Know

sritcp
Posts: 431
Joined: Tue Jun 05, 2012 5:38 pm

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

Post by sritcp » Tue Sep 22, 2015 6:28 pm

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

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4172
Joined: Sun Jan 07, 2007 9:12 pm

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

Post by bn » Tue Sep 22, 2015 7:05 pm

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

pink
Posts: 280
Joined: Wed Mar 12, 2014 6:18 pm

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

Post by pink » Tue Sep 22, 2015 7:55 pm

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!
Greg (pink) Miller

MadPink, LLC
I'm Mad, Pink and Dangerous to Know

Post Reply