writing and loading array to and from file

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
propeller
Posts: 22
Joined: Sat Oct 13, 2018 12:22 pm
Location: Austria

writing and loading array to and from file

Post by propeller » Thu Nov 15, 2018 9:08 pm

hi there, its me again :D

as i learn livecode i stubled into another problem. basically i want to find a way to store information in a file and load it again when needed. for my example i made three fields (f1, f2, f3) and a save button. i put the content into an array and save it. this works. and yes i seperated the values of the fields with commas as i am too noob to know better.

i have another three fields (ff1, ff2, ff3) and a load button. i can load the file and answer is showing me the content but i struggle seperating the content and putting it into the seperate fields (ff1, ff2, ff3). so basically i would need to seperate the loaded content in my array by commas.

here is the code for the save button:

Code: Select all

on mouseUp
   local tMultiArray
   
   put fld "f1" into tf1
   put fld "f2" into tf2
   put fld "f3" into tf3
      
   put tf1,tf2,tf3 into tMultiArray
     
   ## Store the fillGradient array for the graphic object in a file
   ask file "Save MultiArray to file:"
   put it into theFile
   
   if theFile is not empty then
      put tMultiArray into URL ("binfile:" & theFile)
   end if
end mouseUp
and here is the code for the load button:

Code: Select all

on mouseUp pButtonNumber
   ## Load fillGradient array from file
   answer file "Select file containing MultiArray:"
   put it into theFile
   
   if theFile is not empty then
      put URL ("binfile:" & theFile) into tArrayLoaded
      answer tArrayLoaded
      put tArrayLoaded[1][1] into fld "ff1"
      put tArrayLoaded[1][2] into fld "ff2"
      put tArrayLoaded[1][3] into fld "ff3"
   end if
   
end mouseUp
Again, it would be very kind of you nice people to help me grow my livecode-brain.

Klaus
Posts: 13806
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: writing and loading array to and from file

Post by Klaus » Thu Nov 15, 2018 9:35 pm

Hi Michael,

well, this:

Code: Select all

...
put fld "f1" into tf1
put fld "f2" into tf2
put fld "f3" into tf3
put tf1,tf2,tf3 into tMultiArray
...
will NOT create an array of any kind, just the content of your threee field, separated by comma! 8)

You should do this:

Code: Select all

...
## We create a one dimensional array_
put fld "f1" into tMultiArray[1]
put fld "f2" into tMultiArray[2]
put fld "f3" into tMultiArray[3]
...
Now you also have to ARRAYENCODE it before saving to disk:

Code: Select all

...
## Store the fillGradient array for the graphic object in a file
ask file "Save MultiArray to file:"
put it into theFile   
if theFile is not empty then
    put arrayencode(tMultiArray) into URL ("binfile:" & theFile)
end if
...
Then later use this to import the array data back into your stack:

Code: Select all

...
 if theFile is not empty then
      put URL ("binfile:" & theFile) into tArrayLoaded
      ## You cannot display ARRAYs without some pre-work!
      ## answer tArrayLoaded
      
      ## DE-code array:
      put arraydecode(tArrayLoaded) into tArrayLoaded

      ## We created a SINGLE dimensional array, so we have to use:
      put tArrayLoaded[1] into fld "f1"
      put tArrayLoaded[2] into fld "f2"
      put tArrayLoaded[3] into fld "f3"
   end if
 ...
Best

Klaus

propeller
Posts: 22
Joined: Sat Oct 13, 2018 12:22 pm
Location: Austria

Re: writing and loading array to and from file

Post by propeller » Thu Nov 15, 2018 9:49 pm

Thank you, Klaus! You know your stuff.

mrcoollion
Posts: 719
Joined: Thu Sep 11, 2014 1:49 pm
Location: The Netherlands

Re: writing and loading array to and from file

Post by mrcoollion » Fri Nov 16, 2018 8:32 am

Just a small tip.

Make sure that the filename does not contain characters like * .
It took me an hour to figure out why a file with an array in it was not created until I took out the *** from the filename. :mrgreen:

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”