Creating variable/custom property names via scripts

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
Clarkey
Posts: 109
Joined: Fri Jun 11, 2010 11:10 am

Creating variable/custom property names via scripts

Post by Clarkey » Sun Jun 27, 2010 5:27 pm

Hi folks, Is it possible to create variables and/or custom properties 'on the fly', through scripting - or do all variables have to be declared in advance? I want to convert a list (of unknown length) with lines containing 2 items: thing & notes...

Fred, "notes about Fred'
John, "Notes about John'
Kate, "Notes about Kate"

...into a series of custom properties called uFred, uJohn and uKate, with the respective notes item becoming the contents of the custom property.

I'm wondering if I can use some kind of 'repeat for each line' loop where for each line, a new custom property "uFred" is created and then the "notes" can be set into the custom property(?)

The example is a simplification. In reality, item 1 of each line is not "Fred", "John" or "Kate" but the search string used for a series of SOAP calls and item 2 is the XML tree ID for the returned message - from which I will place the XMLText into the custom property contents. However, I thought this example might make more immediate sense, as it's the automated naming of new variables or custom properties that is the main question.

And, if there's a better way to do this, please put me straight.
Best,
Keith..

Clarkey
Posts: 109
Joined: Fri Jun 11, 2010 11:10 am

Re: Creating variable/custom property names via scripts

Post by Clarkey » Sun Jun 27, 2010 5:30 pm

...and I forgot to mention that the reason I need to create the names "Fred", etc on the fly, is because this list is the result of another SOAP call - so I don't know how many names are involved (and I need to manage changes in the list of names - hence the persistence of custom properties).
Best,
Keith..

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

Re: Creating variable/custom property names via scripts

Post by bn » Sun Jun 27, 2010 7:17 pm

Keith,
if I get you right than this sounds like it is something for an array. You than can store the whole array in a custom property and retrieve it from there.

suppose your SOAP searches are unique and you get them in a line with two items that are delimited by comma.

Code: Select all

on mouseUp
   put field 1 into tSearchResult -- adjust your data source
   put "" into tMySoapArray --clear / initialize array. not necessary but nice
   
   repeat for each line aLine in tSearchResult
      put item 2 of aLIne into tMySoapArray[item 1 of aLine]
   end repeat
   set the uSoapSearch of this card to tMySoapArray --now the array is in the custom property
   -- lets take it out again and see
   put the uSoapSearch of this card into tMyTestArray
   put the keys of tMyTestArray into tKeys -- a list with on each line one key (john etc)
   put tKeys -- put them in the message box
   wait 3 seconds with messages -- so we can look at them
   
   -- now we destroy the array to see it in a readable form
   combine tMyTestArray by return and comma -- the array is no longer an array but a 'normal' variable
   put tMyTestArray -- lets see it in the message box
end mouseUp 
in field 1 I put :
fred,123
john,234
mary,345
Just remember order of the keys of an array dont reflect the order in which they were created.

This is a very fast way to create and keep your data additionally it is probably easier to maintain than a lot of custom properties. The access of custom properties consumes a little time so I avoid accessing them too often like in a repeat loop, just take them out and work on a variable/script local variable. For arrays it is the only way: you have to take them out of the custom property anyways

this is just a little example code in case you are not familiar with arrays. It looks to me that you are going to love them for the kind of things you do. This is a one-dimensional version of an arry, which might be enough for what you describe, multidimensional arrays are even more powerful but also a bit more work to maintain.
regards
Bernd

Clarkey
Posts: 109
Joined: Fri Jun 11, 2010 11:10 am

Re: Creating variable/custom property names via scripts

Post by Clarkey » Mon Jun 28, 2010 9:07 am

Hi Bernd,
Thanks for another piece of enlightenment! :D
I was definitely going down the wrong path and you are spot-on with the use of arrays.

Coming from a non-development background, I didn't really understand the difference between an array and a list of delimited items but thanks to your explanation and example code, I now see that arrays work in a similar way to simple, 2-column database tables, with item 1 as the unique row ID. Plus, there's the added benefit of array-specific functions and nesting, which will help enormously, as I start to unravel the returned XML!

And, another section of the user guide is now starting to make sense! :wink:

I note your comment about row order not reflecting create order (like a database). I see there's no array sort function, so am I correct to assume that an array defaults to alphabetical order by item 1?

Brilliant! Your help is much appreciated.
Best
Keith..

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

Re: Creating variable/custom property names via scripts

Post by bn » Mon Jun 28, 2010 4:35 pm

Hi Keith,
glad to be of help.
I see there's no array sort function, so am I correct to assume that an array defaults to alphabetical order by item 1?
if you ask for the keys of an array there is no order in them, neither numerical nor alphabetical.

Since you ask: things get a little complicated if you want to access the data in a particular order.
Skip this post if you dont really need to access the array in a particular order. It

Just in case I made up a script that creates a nested (multidimensional) array, with one subKey being a sort number.

Make a new stack with 3 fields and a button
script of button:

Code: Select all

-- make the array a script local variable so the function "MySpecialSortFunction" can access it
-- use globals only if you absolutely have to, prefer script local variables or custom properties instead 

local sMyArray 

on mouseUp
   put field 1 into tData
   
   -- we build an array with two subKeys, theID and SortNumber
   -- the sortNumber could be e.g. the milliseconds to have a time stamp to sort on
   -- or a sequential numbering scheme
   -- here we just fake the SortNumber and take it from item 3 of the data in field 1
   
   repeat for each line aLIne in tData
      put item 2 of aLine into sMyArray[item 1 of aLine]["TheID"]
      put item 3 of aLine into sMyArray[item 1 of aLine]["SortNumber"]
   end repeat
   
   -- the primary keys are taken into tKeys
   -- those are the ones we want to sort 
   put the keys of sMyArray into tKeys
   
   -- now some Rev vodoo to sort the keys by "each" with a custom function
   -- in reverse order of creation (descending)
   sort tKeys descending numeric by MySpecialSortFunction(each)
   
   -- tKeys are now sorted last to first-> put them into field 2
   put tKeys into field 2
   
   -- lets retrieve some data from the array in the reverse order
   -- and put it into tCollect 
   put "" into tCollect
   
   repeat for each line aLine in tKeys
      put aLine & comma && sMyArray[aLIne]["TheID"] & return after tCollect
   end repeat
   delete char -1 of tCollect -- trailing return
   
   -- tCollect has now the primary keys (just to make shure) and the ids in reverse order
   put tCollect into field 3
   
end mouseUp

function MySpecialSortFunction pAKey
   -- pAKey is a primary key of the array
   -- we just return the content of subKey "SortNumber" of the array
   -- and the sort routine uses it as the sorting criterion
   return sMyArray[pAKey]["SortNumber"]
end MySpecialSortFunction
in field 1 you put
John,1,234
Mary,2,345
Jonathan,3,456
see the comments in the code.

for a one dimensional array the key being lets say a name and the content of this key is a sequential number
it is a lot easier to get to the sort order. Just "combine" the array by return and comma and do a

Code: Select all

sort lines of myCombinedArray descending numeric by item 2 of each
this is fairly easy. Item 1 of each line of that list would be in the desired order.

Maybe someone has an easier way to sort a multi-dimensional array?

dont let all this confuse you, you will get there when you need it.

regards
Bernd

Clarkey
Posts: 109
Joined: Fri Jun 11, 2010 11:10 am

Re: Creating variable/custom property names via scripts

Post by Clarkey » Mon Jun 28, 2010 5:17 pm

Hi Bernd,
I followed your previous thread and now have an array with the full, raw XMLText call responses, mapped against calling string keys. When I called the array keys back out to test it was clear that they were in no apparent order! :D

Yes, your latest response is a little more challenging - though I think I can follow the concept, if not the code. I'll come back to it when I'm further up the learning curve or if I find I simply can't progress without a known key order - but I'll try to avoid that need! :wink:

The next challenge is decomposing the XML into a useful, multi-dimensional array structure - but this RevUp newsletter and the associated stack are proving very insightful http://www.runrev.com/newsletter/july/i ... etter1.php
Best,
Keith..

Post Reply