Alternate Form of printKeys()

Bob Sneidar bobs at twft.com
Mon Jan 30 12:54:17 EST 2012


Thanks for the bug report I will look into it. 

The usefulness of this is not so  that it will be readable. It is so that you can use the filter command on the result, then recreate the array with the other function. Let's say I have a datagrid that came from a query that returned 10,000 records from a remote SQL database. I also have a search field where after typing some text and then pausing, I want to filter all the columns of data based upon that text. 

If building a query, I would have to loop through the columns and build a query using IN() for every column. Then I would have to requery the database. As the user types and pauses, I would have to do this every time. 

What I have done instead is the first time the user pauses, I convert the datagrid array to the text form and store it as a global variable. Now when the user pauses, I get a copy of the data and filter the text based upon what the user typed. I then recreate the array from the filtered text and set the dgdata of the datagrid to the resulting array. This makes it very quick, especially with SQL servers that do not respond all that quickly. 

There are other applications though. Ever want to subtract one array from another? Delete a single key of multidimensional array? Rename a key? Also, in the original PrintKeys() function, if the value contained multiple lines, it would only return the first line. My function returns all the lines. It also replaces tabs and returns with placeholder ascii characters. 

I am sure you could do all these things by looping through array elements, but it seems like it would be quicker (and less programming) to convert the entire array to text and then run single filter commands on the result. 

Bob


On Jan 28, 2012, at 5:35 AM, Peter M. Brigham, MD wrote:

> One little tweak:
> 
> instead of 
> 
>>       if theKey is not a number then
>>           replace theKey with quote & theKey & quote in theKeyList
>>       end if
> 
> use 
> 
> if theKey is not a number then
>   replace "[" & theKey & "]" with "[" & quote & theKey & quote & "]" in theKeyList
> end if
> 
> I tried it with keys like [second][c][2] and your original replacement line gave me ["se"c"ond"] as one of the entries.
> 
> What I liked about the original handlers was that the previous altPrintKeys would output a textual representation of the array in an intuitive outline form, so you could see at a glance the structure and contents of the array. Your new function converts an array to a text string but it's really hard to tell by looking at the string how the array is structured, so it has limited value to me. For what purpose would you be using the new version of these functions?
> 
> -- Peter
> 
> Peter M. Brigham
> pmbrig at gmail.com
> http://home.comcast.net/~pmbrig
> 
> 
> On Jan 26, 2012, at 6:12 PM, Bob Sneidar wrote:
> 
>> Here are the functions. I have tested them and they seem to be fine, but any bug reports would be appreciated. Note that there is another distinct advantage of these functions over the original printKeys(): These functions allow for returns and tabs in the values. I convert them to ascii(30) and ascii(11) respectively when I return text and back again when I return an array. 
>> 
>> Also bear in mind I do no error checking. Who knows what would happen if you pass something other than what is expected. Hang on to your socks! :-)
>> 
>> function altPrintKeys @pArray, theKeyList, pFullData
>>   put numtochar(11) into vertTab
>>   put numtochar(30) into altCr
>>   put the keys of pArray into theKeys
>>   sort theKeys numeric
>> 
>>   repeat for each line theKey in theKeys
>>       put "[" & theKey & "] " after theKeyList
>>       if theKey is not a number then
>>           replace theKey with quote & theKey & quote in theKeyList
>>       end if
>>       if pArray[theKey] is an array then
>>           put pArray[theKey] into theTempArray
>>           put altPrintKeys(theTempArray, theKeyList, pFullData) after theText
>>           put empty into the last word of theKeyList
>>           delete the last char of theKeyList
>>           put cr into the last char of theText
>>       else
>>           put "pArray " & the last word of theKeyList into theKeyName
>>           -- put "put " & theKeyName & " into theValue" into theCommand
>>           -- do theCommand
>>           put the value of theKeyName into theValue
>>           replace tab with vertTab in theValue
>>           replace return with altCr in theValue
>>           put theKeyList & tab & theValue & comma after theText
>>           put empty into the last word of theKeyList
>>           delete the last char of theKeyList
>>       end if
>>   end repeat
>> 
>>   return theText
>> end altPrintKeys
>> 
>> function altKeysToArray theText
>>   put numtochar(11) into vertTab
>>   put numtochar(30) into altCr
>>   repeat for each line theRecord in theText
>>       repeat for each item theKeyData in theRecord
>>           put the itemdelimiter into theOldDelim
>>           set the itemdelimiter to tab
>>           put item 1 of theKeyData into theKeyList
>>           put item 2 of theKeyData into theValue
>>           replace vertTab with tab in theValue
>>           replace altCr with return in theValue
>>           set the itemdelimiter to theOldDelim
>>           put "put " & quote & theValue & quote & " into theArrayA " & theKeylist into theCommand
>>           do theCommand
>>       end repeat
>>   end repeat
>> 
>>   return theArrayA
>> end altKeysToArray
>> _______________________________________________
>> use-livecode mailing list
>> use-livecode at lists.runrev.com
>> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
>> http://lists.runrev.com/mailman/listinfo/use-livecode
> 
> 
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode





More information about the use-livecode mailing list