Hello to all,
First, congratulations to Marek, and thank you for sharing the idea.
I had a situation that I preferred instead of recording in data base the open orders in to a resturant, leave the data in memory, but predicting a power outage. How I work with vectors, I came across the necessity of save to disk every change, in order to retrieve the invoices not closed in case of interruption.
Immediately I remembered of the your contribution you had published, and Pablo Cesar published in the Brazilian forum too, but got weird in my case to save as much as to restore, because his option transforms each element in block then back for array.
First, I tried to use the functions ValToPrgExp () and PrgExpToVal (), but I can´t found in what lib they are. So, I decided to use the functions Hb_Serialize () and Hb_DeSerialize ().
below is the example. I hope you like:
Code: Select all
/*
To Save Array to disk using the functions:
Hb_Serialize(), Hb_DeSerialize(), Hb_MvSave() and Hb_MvRestore()
Note: See that all variables are more than 10 characters due to is possible
to save and to restore long variables with the funcions Hb_MvSave()
and Hb_MvRestore().
*/
#include "minigui.ch"
Function Main()
/*
Here can be declared as Local, but let´s to declare as Private because
the command Release can´t afect Local or Static variebles.
*/
Private aPrimeiraMatriz := {}
Private aSegundaMatriz := {}
Private aMatrizMaster := {}
/*
This need to be declared as Private or Public, because you can´t
save variables Local, Static, Array or Block
*/
Private cMatrizSerializada
/*
Let´s to create the 1st Multi-dimensional Array:
*/
AaDd( aPrimeiraMatriz, { Date() + 1, "Campinas", 123.45 } )
AaDd( aPrimeiraMatriz, { Date() + 2, "Paulinia", 123.46 } )
AaDd( aPrimeiraMatriz, { Date() + 3, "Sao Paulo", 123.47 } )
AaDd( aPrimeiraMatriz, { Date() + 4, "Rio de Janeiro", 123.48 } )
/*
Let´s to create the 2nd Multi-dimensional Array:
*/
AaDd( aSegundaMatriz, { Date() + 5, "Andre", "Paulinia", 1994 } )
AaDd( aSegundaMatriz, { Date() + 6, "Carolina", "Paulinia", 1996 } )
AaDd( aSegundaMatriz, { Date() + 7, "Pedro", "Paulinia", 1998 } )
AaDd( aSegundaMatriz, { Date() + 8, "Beatriz", "Paulinia", 2002 } )
/*
For convenience, Let´s to creat a 3rd Multi-dimensional Array for
to insert the Arrays that we want to save:
*/
AaDd( aMatrizMaster, { aPrimeiraMatriz, aSegundaMatriz } )
/*
For lets to save, let´s to serialize the 3rd Array.
The result is a character type variable:
*/
cMatrizSerializada := Hb_Serialize( aMatrizMaster )
/*
Save in file. If the extension is omited, the default is .hbv:
*/
Hb_MvSave( "MeuArray.Mem", "cMatrizSerializ*" )
/*
let´s delete all variables in this example:
*/
Release All
/*
We shall prove that have been deleted:
*/
If Type( "aMatrizMaster" ) = "U"
MsgInfo( [Variable "aMatrizMaster" don´t exist!], "No variable in memory" )
Endi
/*
let´s restore the variables saved to disk:
*/
Hb_MvRestore( "MeuArray.Mem", .T. )
/*
Let´s deserialize the serialized variable to it back to be an
Array again:
*/
aMatrizMaster := Hb_DeSerialize( cMatrizSerializada )
/*
let´s to delete the variable "cMatrizSerializada", due to it
is unnecessary from now:
*/
Release cMatrizSerializada
/*
Let's rebuild our Arrays again:
*/
aPrimeiraMatriz := aMatrizMaster[ 1, 1 ]
aSegundaMatriz := aMatrizMaster[ 1, 2 ]
/*
Now we shall prove that our Arrays have been restored:
*/
MsgInfo( Padc( hb_ValToExp( aPrimeiraMatriz[ 1, 2 ] ), 50 ), "aPrimeiraMatriz [1,2]" )
MsgInfo( Padc( hb_ValToExp( aSegundaMatriz[ 2, 3 ] ), 50 ), "aSegundaMatriz [2,3]" )
MsgInfo( Padc( hb_ValToExp( aMatrizMaster[ 1, 1, 2 ] ), 50 ), "aMatrizMaster [1, 1, 2]" )
MsgInfo( Padc( hb_ValToExp( aMatrizMaster[ 1, 2, 2, 3 ] ),50 ), "aMatrizMaster [1, 2, 2, 3]" )
Return Nil