Page 1 of 1

Storing data in an external file

Posted: Tue Jun 04, 2013 3:13 pm
by MaxV
Hello,
I read here and there about storing data, and I need to store just 4 data in an external file. I think that the best way is to save all properties of an object.
Is there any way to save an object?
An object has custom properties, if I could save an object, I could load it and all my data would be perfectly parsed for retrieving data.
Lets see the following example:

Code: Select all

set the owner of field "car" to "Max"
set the model of field "car" to "Ferrari"
set the age of field "car" to  2

Re: Storing data in an external file

Posted: Tue Jun 04, 2013 3:57 pm
by Klaus
Hi Max,

no way to store a single object in an external file, sorry!
Hint: "owner" is a reserved word in Livecode, so you shopuld change the name for that property!

Best

Klaus

Re: Storing data in an external file

Posted: Tue Jun 04, 2013 4:36 pm
by MaxV
I thought also about array:

Code: Select all

put "Max" into  car["owner"]
put "Ferrari" into  car["model"]
put 2 into car["age"]
but how can I save an array in a file and retrieve array data from a file?

Re: Storing data in an external file

Posted: Tue Jun 04, 2013 4:37 pm
by jmburnod
Hi MaxV,
You can get all properties of an object, export it in a file and import it to create this object.
No tested but should work.

Code: Select all

on mouseUp
   put the short id of the target into tID
   put GetPropControl(tID)  into fld "fProp"
end mouseUp

function GetPropControl pControlID 
   put empty into tPropControl
   put the properties of control id pControlID into tArray
   put the keys of tArray into tKeys
   repeat for each line tProp in tKeys
      put tProp & tab & tarray[tProp] & cr after tPropControl
   end repeat
   delete char -1 of tPropControl
   return tPropControl
end GetPropControl
And you can do the same for the customproperties

Best regards
Jean-Marc

Re: Storing data in an external file

Posted: Tue Jun 04, 2013 4:52 pm
by MaxV
Wow, I found a solution here: http://lessons.runrev.com/s/lessons/m/4 ... e-it-again
I can use arrayEncode and arrayDecode!!! :-)

Re: Storing data in an external file

Posted: Tue Jun 04, 2013 6:44 pm
by FourthWorld
You could use a stack file for that, setting the filename property and then using the save command.

But perhaps just as simple would be to work with an array while in memory, saving it to disk with arrayEncode, to read later after passing it through arrayDecode.

Re: Storing data in an external file

Posted: Wed Jun 05, 2013 11:04 am
by Mark
Jean-Marc, the properties property doesn't include all properties of an object. Therefore, I'm not entirely sure that it can be used to recreate an object entirely. Sometimes it works, sometimes it doesn't, depending on which properties you have changed and which object you want to save.

MaxV, that's a great find. ArrayEncode and ArrayDecode may do what you need. Here's a simple and short script, which does what you want:

Code: Select all

on mouseUp
   put the long id of fld "Car" into myField
   put "C:\folder\file.enc" into myFilePath
   put arrayEncode(the customproperties of myField) into myEncArray
   put myEncArray into url ("binfile:" & myFilePath)
end mouseUp

on mouseUp
   put the long id of fld "Car" into myField
   put "C:\folder\file.enc" into myFilePath
   put arrayDecode(url ("binfile:" & myFilePath)) into myArray
   set the customproperties of myField to myArray
end mouseUp
You'll have to change the file path in each handler.

Kind regards,

Mark