Saving array into mem file

General Help regarding HMG, Compilation, Linking, Samples

Moderator: Rathinagiri

Post Reply
User avatar
mol
Posts: 3219
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Has thanked: 224 times
Been thanked: 125 times
Contact:

Saving array into mem file

Post by mol » Mon Mar 25, 2013 8:43 pm

Hi!
I want to share my idea about saving array into mem file by

Code: Select all

save all like xMyVar toMyMemFile
As you know, you can't save array variable into mem file. The name of variable is also limited to 10 characters (I've tested it :))
First, I've trid to create many variables for each array cell.
But, it was little stupid.
I've declared text variable, eg cTmp.
Then, I've set it's value to:

Code: Select all

cTmp := "{|| aArrayToSave:=" + hb_valtoexp(aArrayToSave) + "}"
this variable, as a text var can be saved by save command:

Code: Select all

save all like cTMP to MyMemFile
to get my array back from disk, I need to do it in this way:

Code: Select all

restore from MyMemFile additive
eval(&cTmp)
You can save this way few tables in one variable:

Code: Select all

cTmp := "{|| aA1:=" + hb_valtoexp(aA1) +","+ aA2:=" + hb_valtoexp(aA2) +","+aA3:=" + hb_valtoexp(aA3) + "}"

Be careful about array size!
Nice testing!
Regards, Marek
Last edited by mol on Tue Mar 26, 2013 6:25 am, edited 1 time in total.

User avatar
esgici
Posts: 4513
Joined: Wed Jul 30, 2008 9:17 pm
DBs Used: DBF
Location: iskenderun / Turkiye
Has thanked: 389 times
Been thanked: 111 times
Contact:

Post by esgici » Mon Mar 25, 2013 10:40 pm

Thanks to share :)

It's a new load/save array method.

TIA :D
Viva INTERNATIONAL HMG :D

User avatar
danielmaximiliano
Posts: 2146
Joined: Fri Apr 09, 2010 4:53 pm
Location: Argentina
Has thanked: 162 times
Been thanked: 25 times
Contact:

Post by danielmaximiliano » Mon Mar 25, 2013 10:47 pm

Interesting, thanks for sharing
*´¨)
¸.·´¸.·*´¨) ¸.·*¨)
(¸.·´. (¸.·` *
.·`.HMG : It's magic !
(¸.·``··*

Saludos / Regards
DaNiElMaXiMiLiAnO

Whatsapp. : +54901169026142
Telegram Name : DaNiElMaXiMiLiAnO

User avatar
Pablo César
Posts: 4059
Joined: Wed Sep 08, 2010 1:18 pm
Location: Curitiba - Brasil
Has thanked: 100 times
Been thanked: 182 times

Post by Pablo César » Tue Mar 26, 2013 4:07 am

Great Marek !

I made a example with a very litle differance:

Code: Select all

#include <hmg.ch>

Function Main
Local aA1,aA2,aA3

If !File("MyMemFile.mem")
   aA1:={1,"Test1",CtoD("01/01/2013"),.T.}
   aA2:={2,"Test2",CtoD("02/01/2013"),.F.}
   aA3:={3,"Test3",CtoD("03/01/2013"),.T.}
   
   MsgInfo(hb_ValToExp(aA1), "Array aA1 Just assigned")
   MsgInfo(hb_ValToExp(aA2), "Array aA2 Just assigned")
   MsgInfo(hb_ValToExp(aA3), "Array aA3 Just assigned")

   Private cTmp1 := "{|| aA1:=" + hb_valtoexp(aA1) +"}"
   Private cTmp2 := "{|| aA2:=" + hb_valtoexp(aA2) +"}"
   Private cTmp3 := "{|| aA3:=" + hb_valtoexp(aA3) +"}"

   Save all Like cTMP* to MyMemFile
Endif

aA1:={}
aA2:={}
aA3:={}
Restore from MyMemFile Additive
aA1:=Eval(&cTmp1)
aA2:=Eval(&cTmp2)
aA3:=Eval(&cTmp3)

MsgInfo(hb_ValToExp(aA1), "Array aA1 recovered")
MsgInfo(hb_ValToExp(aA2), "Array aA2 recovered")
MsgInfo(hb_ValToExp(aA3), "Array aA3 recovered")
Return Nil
It´s shows perfectly recovered values thru mem file, all with it's own types of each elements. Very good Marek, congrats !
HMGing a better world
"Matter tells space how to curve, space tells matter how to move."
Albert Einstein

User avatar
mol
Posts: 3219
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Has thanked: 224 times
Been thanked: 125 times
Contact:

Post by mol » Tue Mar 26, 2013 6:24 am

THX for your interesting!

User avatar
Jairo Maia
Posts: 53
Joined: Sun Jul 17, 2011 1:47 pm
Location: Campinas - SP - Brazil

Post by Jairo Maia » Thu May 09, 2013 2:34 pm

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

Leopoldo Blancas
Posts: 388
Joined: Wed Nov 21, 2012 7:14 pm
Location: México
Has thanked: 3 times
Been thanked: 9 times

Post by Leopoldo Blancas » Thu May 09, 2013 3:22 pm

Gracias por sus aportaciones...
*--------------------------------------------------------
Thank you for your contributions ...

Polo

Post Reply