'Flattening'/storing arrays

Jan Schenkel janschenkel at yahoo.com
Mon Dec 16 17:16:01 EST 2002


Hi Jim and Dar,

Appearently I left a bit of an error in my quick&dirty
array-to-xml scheme (I forgot to 'enquote' the keys).
Here's a better version of the serialiation function:

function serialisedArray @pArray
  put "<array>" & return into tSerialisedArray
  put the keys of pArray into tKeys
  repeat for each line tKey in tKeys
    put "<element key=" & quote & tKey & quote & \
    ">" & pArray[tKey] & "</element>" & return \
    after tSerialisedArray
  end repeat
  put "</array>" after tSerialisedArray
  return tSerialisedArray
end serialisedArray

And to boot, here's a function to de-serialise the
string into an array.

function deSerialiseArray pString
  if line 1 of pString is not "<array>" or \
      line -1 of pString is not "</array>" then
    return "Error: invalid string"
  end if
  put 0 into tStart
  get offset("<element key=" & quote, pString, tStart)
  repeat until it = 0
    -- first retrieve the key for the element
    put tStart + it + 14 into tKStart
    put offset(quote & ">", pString, tKStart) \
        into tKEnd
    if tKEnd = 0 then return "Error: invalid string"
    put char tKStart to (tKStart + tKEnd - 1) \
        of pString into tKey
    -- then retrieve the data for the element
    put tKStart + tKEnd + 2 into tDStart
    put offset("</element>", pString, tDStart) \
        into tDEnd
    if tDEnd = 0 then return "Error: invalid string"
    put char tDStart to (tDStart + tDEnd - 1) \
        of pString into tData
    -- store the data in the element with the key
    put tData into tArray[tKey]
    -- and get ready for the next retrieve cycle
    put tDStart + tDEnd + 10 into tStart
    get offset("<element key=" & quote, pString, \
       tStart)
  end repeat
  -- pString has now been de-serialised into tArray
  return tArray
end deSerialiseArray

However, as Dar pointed out, if the data is 'polluted'
in any way (as in: contains data that might mess up
the XML-structure) then this is not a good method,
though it's arguable that you'll have trouble storing
and retrieving the data outside of custom properties
in such cases anyway.

Hope this helped,

Jan Schenkel.

=====
"As we grow older, we grow both wiser and more foolish at the same time."  (La Rochefoucauld)

__________________________________________________
Do you Yahoo!?
Yahoo! Mail Plus - Powerful. Affordable. Sign up now.
http://mailplus.yahoo.com



More information about the Use-livecode mailing list