Page 1 of 1

encoding and decoding multilevel arrays

Posted: Wed Oct 30, 2019 12:17 pm
by danielrr
Hi all

I seem to be unable to arrayDecode an array containing more than two "levels"

Code: Select all

   put "one" into arrayBibliografia[1]
   put "foo" into arrayBibliografia[1]["one"]
   put "two" into arrayBibliografia[2]
   put "three" into arrayBibliografia[3]
   put "binfile:test" into nfile 
   open file nfile for write 
   get the result 
   if the result <> "" then errorFin "error al crear un archivo " & it. --it's ok up to this point, Daniel
   write arrayEncode(arrayBibliografia,"7.1") to file nfile
   close file nfile
   get the result
   if the result <> "" then errorFin "error al crear un archivo " & it
 open file nfile
   read from file nfile until eof
   close file nfile
   put arrayDecode(it) into arrayResult

At the end of this code, the output is puzzling. The contents of "one" in arrayResult is not "foo". Instead, contents of "1" and "3" are empty and content of "2" is "1869505798" ( :shock: )

The problem is worst if the array contains unicode chars. In that case the result can be erratic (as in the example above) of the arrayDecode function may not work at all

Re: encoding and decoding multilevel arrays

Posted: Wed Oct 30, 2019 1:25 pm
by [-hh]

Re: encoding and decoding multilevel arrays

Posted: Wed Oct 30, 2019 2:59 pm
by danielrr
Thanks. Does it mean that there's no solution to the problem but to convert the array into a variable using a repertoire of delimiters before saving it to a file, and then turn again the variable into an array using the same repertoire of delimiters?

Re: encoding and decoding multilevel arrays

Posted: Wed Oct 30, 2019 3:35 pm
by [-hh]
With a second read I noticed that you only wish to decode/encode the array, not flatten it, sorry.
Your problem may be the incomplete binary write/read. So you could try, which is moreover 'simpler':

write:

Code: Select all

put arrayEncode(arrayBibliografia,"7.1") into url ("binfile:"&stackFolder()&"test.data")
read:

Code: Select all

put arrayDecode(url ("binfile:"&stackFolder()&"test.data")) into dd
This works here (e.g. dd[1]["one"] is "foo").

__________________________________
I used also

Code: Select all

function stackFolder
   put the effective filename of this stack into f
   set itemdelimiter to slash; put empty into last item of f
   return f
end stackFolder

Re: encoding and decoding multilevel arrays

Posted: Wed Oct 30, 2019 4:05 pm
by FourthWorld
The output of arrayEncode is binary. The key to reading/writing those files is to use binary mode, e.g.:

Code: Select all

open file "whatever.lson" for binary write
...or the "binfile" option for the "put URL..." syntax as hh showed.

LSON files (LiveCode encoded arrays) are wonderfully robust and efficient, a good choice for many storage needs.

Re: encoding and decoding multilevel arrays

Posted: Wed Oct 30, 2019 5:04 pm
by danielrr
Works flawlessly, even with Unicode strings, thanks -hh, FourthWorld :D