encoding and decoding multilevel arrays

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
danielrr
Posts: 140
Joined: Mon Mar 04, 2013 4:03 pm

encoding and decoding multilevel arrays

Post by danielrr » Wed Oct 30, 2019 12:17 pm

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

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: encoding and decoding multilevel arrays

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

shiftLock happens

danielrr
Posts: 140
Joined: Mon Mar 04, 2013 4:03 pm

Re: encoding and decoding multilevel arrays

Post by danielrr » Wed Oct 30, 2019 2:59 pm

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?

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: encoding and decoding multilevel arrays

Post by [-hh] » Wed Oct 30, 2019 3:35 pm

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
shiftLock happens

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9833
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: encoding and decoding multilevel arrays

Post by FourthWorld » Wed Oct 30, 2019 4:05 pm

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.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

danielrr
Posts: 140
Joined: Mon Mar 04, 2013 4:03 pm

Re: encoding and decoding multilevel arrays

Post by danielrr » Wed Oct 30, 2019 5:04 pm

Works flawlessly, even with Unicode strings, thanks -hh, FourthWorld :D

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”