Missing split option?

Richard Gaskin ambassador at fourthworld.com
Wed May 18 12:47:45 EDT 2016


Bob Sneidar wrote:

 > On May 18, 2016, at 08:37 , Richard Gaskin wrote:
 >
 >> DGs will give the array directly so I don't need to deal with that
 >> when using those as the Master, but more often I use list fields
 >> for the Master, where it would be convenient to have the array made
 >> in one line.
 >
 > I was thinking of having a hidden data grid for the sole purpose of
 > converting your data to an array. The repeat loop you are looking
 > for may already been written in the datagrid library, which begs the
 > possibility, why not get that function from the datagrid library
 > itself? Might have some methods that will prove useful.

There's a lot of great stuff in the DG code, but this one was easy 
enough to write:

function NamedRowToArray pData
    set the itemdel to tab
    put line 2 of pData into tVals
    put 0 into i
    repeat for each item tKeyName in line 1 of pData
       add 1 to i
       put item i of tVals into tA[tKeyName]
    end repeat
    return tA
end NamedRowToArray

On sample data of 100 fields each with values of > 100 chars (a bit 
larger than most of the data I work with), it returns the array in the 
format I'm looking for in about 1.5 ms, fast enough for UI-related tasks.

Interestingly (though only mildly) I was able to shave off about 1/10th 
of a millisecond by pre-splitting the values rather than using item chunks:

function NamedRowToArray pData
    set the itemdel to tab
    put 0 into i
    put line 2 of pData into tVals
    split tVals by tab
    put line 1 of pData into tKeyNames
    repeat for each item tKeyName in tKeyNames -- line 1 of pData
       add 1 to i
       put tVals[i] into tA[tKeyName]
    end repeat
    return tA
end NamedRowToArray

Even more interesting, that split is only beneficial when dealing with a 
lot of values.  When I reduced the sample set from 100 fields to 10, it 
actually added an extra 1/10th of a millisecond to the time 
(understandable since split requiring chunking anyway, plus a little 
hashing time).

Since the minor difference with pre-splitting the values is negligible 
and data-size dependent, for now I'll stick with the first version since 
to my eye it's more straightforward.

-- 
  Richard Gaskin
  Fourth World Systems
  Software Design and Development for the Desktop, Mobile, and the Web
  ____________________________________________________________________
  Ambassador at FourthWorld.com                http://www.FourthWorld.com





More information about the Use-livecode mailing list