Author Topic: Saving arrays  (Read 4032 times)

Offline Richard Rae

  • Mr. Drawsprite
  • **
  • Posts: 79
    • View Profile
Saving arrays
« on: 2005-Nov-06 »
How would you save an array to a file and then reload that array later.

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10711
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Saving arrays
« Reply #1 on: 2005-Nov-07 »
how many dimensions?

Offline Robert

  • Mc. Print
  • *
  • Posts: 34
    • View Profile
Re: Saving arrays
« Reply #2 on: 2016-Apr-18 »
I have also this question regarding a 4 dimension array.

Offline Robert

  • Mc. Print
  • *
  • Posts: 34
    • View Profile
Re: Saving arrays
« Reply #3 on: 2016-Apr-18 »
To be more specifik. Is there any method to create files with a loop or a method to save data in another form? I have thought about save in a bitmap.

In dark basic there is a command to save an array in a file so it should be possible. 

Offline nabz32

  • Community Developer
  • Dr. Type
  • ******
  • Posts: 307
    • View Profile
Re: Saving arrays
« Reply #4 on: 2016-Apr-18 »
you mean like

Code: GLBasic [Select]
FOR x = 0 to bounds( 0 , data[] ) - 1

FOR y = 0 to bounds( 1 , data[] ) - 1

readbyte fileChannel ,  data[x][y]

NEXT

NEXT

?
« Last Edit: 2016-Apr-18 by nabz32 »

Offline Ian Price

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 4147
  • On the shoulders of giants.
    • View Profile
    • My Apps
Re: Saving arrays
« Reply #5 on: 2016-Apr-18 »

Yep, look up SAVEWORD, SAVEBYTE, READWORD, READBYTE etc.

Here's a quick example with a 2D array. It sets up an array. Saves the data. Clears the data from array memory. Loads the data, then displays it -

Code: GLBasic [Select]
// --------------------------------- //
// Project: test
// Start: Monday, April 18, 2016
// IDE Version: 12.656


// SETCURRENTDIR("Media") // go to media files

GLOBAL map%[]

DIM map[5][5]

FOR x=0 TO 4
FOR y=0 TO 4
 map[x][y]=RND(9)
NEXT
NEXT

save_data()

clear_data()

load_data()

show_data()


// Save data
FUNCTION save_data:

LOCAL x%, y%

LOCAL file$="mydata.dat"

OPENFILE (1,file$,0)

 FOR y=0 TO 4
 FOR x=0 TO 4
  WRITEWORD 1,map[x][y]
 NEXT
 NEXT

CLOSEFILE 1

ENDFUNCTION
 

// Load data
FUNCTION load_data:

LOCAL x%, y%

LOCAL file$="mydata.dat"

LOCAL ok%=DOESFILEEXIST(file$)

IF ok

 OPENFILE (1,file$,1)

  FOR y=0 TO 4
  FOR x=0 TO 4
   READWORD 1,map[x][y]
  NEXT
  NEXT

 CLOSEFILE 1

ENDIF

ENDFUNCTION


// Clear data
FUNCTION clear_data:

 FOR x=0 TO 4
 FOR y=0 TO 4
  map[x][y]=0
 NEXT
 NEXT
 
ENDFUNCTION


// show loaded data
FUNCTION show_data:

 FOR x=0 TO 4
 FOR y=0 TO 4
  PRINT map[x][y],x*10,y*10
 NEXT
 NEXT

 SHOWSCREEN

 KEYWAIT
 
ENDFUNCTION

 
I came. I saw. I played.

Offline kanonet

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 1142
    • View Profile
    • My GLBasic code archiv
Re: Saving arrays
« Reply #6 on: 2016-Apr-18 »
Actually its aso possible to MEM2SPRITE an array (not sure if only 1dim or also 4dim) and save it as image. At least if we are talking about integer arrays. If you save it as PNG, you get loseless compression.
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

Offline Robert

  • Mc. Print
  • *
  • Posts: 34
    • View Profile
Re: Saving arrays
« Reply #7 on: 2016-Apr-18 »
Ok thanks, its that stright forward. I haven't worked so much with files in GLB and thought it was more complicated.