'Flattening'/storing arrays

Jan Schenkel janschenkel at yahoo.com
Mon Dec 16 15:22:00 EST 2002


--- James Witte <jswitte at bloomington.in.us> wrote:
>   Is there a way to built-in (or already written)
> way to 'flatten' an
> array or otherwise store it in something other than
> a property attached
> to an object (which would neccesitate data files
> being actual stacks)
> 
> Thanks,
> Jim
> 

Hi Jim,

If you can be sure the data in the elements doesn't
contain certain characters, you can use the 'combine'
and 'split' commands to flatten an array into a string
and restore it afterwards.

If that's not the case, you could revert to an XML
serialisation method, which turns your array into some
structure like:

<array>
  <element key="foo">
    whatever is in this element
  </element>
  <element key="bar">
    whatever is in this other element
  </element>
</array>

RunRev 2.0 has excellent XML support so that should be
a breeze to implement. Of course you can serialise it
now with a simple function such as:

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=" & tKey & ">" & \
    pArray[tKey] & "</element>" & return \
    after tSerialisedArray
  end repeat
  put "</array>" after tSerialisedArray
  return tSerialisedArray
end serialisedArray

Remember that if you're working with numbers, upon
transforming a number into a string, the engine will
use the 'numberFormat' property, so you may want to
tweak that before using either method, or apply the
'format' function when you serialise the data.

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