Page 1 of 1

An advice needed to save same variables and its value to mem

Posted: Tue Dec 29, 2020 1:19 pm
by Horizon
Hi,

I have an array like that. First element is variable name, second is variables value.

Code: Select all

{{"Name","Hakan"},
{"Birthday", 15.11.1960},
{"Married", .F.},
{"Tips", {"aaa","bbb","ccc","ddd"}},
{"DoorNumber", 456}}
I want to save this array to one memo field in a record. (DBF) and read again to its variable names.

Is there any simple efficient solution already prepared?

Thanks.

Re: An advice needed to save same variables and its value to mem

Posted: Tue Dec 29, 2020 2:40 pm
by cnavarro

Code: Select all

Function TestArr1( nOpt )

   local aVars := { {"Name","Hakan"}, {"Birthday", "15.11.1960" }, {"Married", .F.}, ;
                    {"Tips", {"aaa","bbb","ccc","ddd"}}, {"DoorNumber", 456} }
   if nOpt = 2
      ?  FW_ValToExp( aVars )
   else
      XBrowse( &(FW_ValToExp( aVars ) ) )

   endif

Return nil
 
But I would surely use a hash to store the variables

Re: An advice needed to save same variables and its value to mem

Posted: Tue Dec 29, 2020 4:58 pm
by Antonio Linares
field->memo := hb_serialize( aVars )

...

aVars := hb_deserialize( field->memo )

Re: An advice needed to save same variables and its value to mem

Posted: Wed Dec 30, 2020 5:55 am
by Horizon
Thank you Mr. Navarro, Antonio,

I will look it.

Re: An advice needed to save same variables and its value to mem

Posted: Wed Dec 30, 2020 2:02 pm
by nageswaragunupudi
Saving and restoring arrays to and from MemoFields of DBF

Using DBFCDX, we can save an array in a memo field like this:

Code: Select all

FIELD->MEMOFIELD := aVars
 
Later, we can read the data into array like this

Code: Select all

aVars := FIELD->MEMOFIELD
 
You can build and run this program to test this:

Code: Select all

#include "fivewin.ch"

REQUEST DBFCDX

function Main()

   local aVars := {{"Name","Hakan"}, ;
                   {"Birthday", {^ 1960/11/15 } }, ;
                   {"Married", .F.}, ;
                   {"Tips", {"aaa","bbb","ccc","ddd"}}, ;
                   {"DoorNumber", 456}}

   SET DATE FORMAT TO "DD.MM.YYYY"

   if !File( "TESTVARS.DBF" )
      DBCREATE( "TESTVARS", {{ "MYVARS", "M", 8, 0 }}, "DBFCDX", .T., "VARS" )
      DBAPPEND()
      CLOSE VARS
   endif

   // save to memofield
   USE TESTVARS VIA "DBFCDX"
   TESTVARS->MYVARS := aVars
   CLOSE TESTVARS

   aVars := nil

   // read array from memofield
   USE TESTVARS VIA "DBFCDX"
   aVars := TESTVARS->MYVARS
   CLOSE TESTVARS

   XBROWSER aVars

return nil
 
Image

This way we can read the saved names and values into an array. I do not think this is what you are looking for. You said:
I want to save this array to one memo field in a record. (DBF) and read again to its variable names.
I understand that you want to read the saved names and values and then create variables with those names and values for use in your application.

This is possible with PUBLIC or PRIVATE variables but not local or static variables.

If what I understand is correct, please see the next post.

Re: An advice needed to save same variables and its value to mem

Posted: Wed Dec 30, 2020 2:04 pm
by nageswaragunupudi
Using SAVE/RESTORE commands:

Before going into saving and restoring variables to/from memo-fields of DBF, let us review the built-in Clipper commands SAVE/RESTORE. These commands are as old as clipper itself and are intended for saving names and values of variables to disk and restoring variables with values later. The values are saved to disk files with extension MEM by default.

Can save and restore PUBLIC and PRIVATE variables with values Character, Date, Logical and Numeric only. Variables with other values like DateTime, Array, Codeblock, etc.

Syntax:

Code: Select all

SAVE TO <memFilename> [ALL [LIKE | EXCEPT <mask>]]
RESTORE FROM <memFilename> [ADDITIVE]
 
This is the test program to save and restore our variables, viz., Name, Birthday, Married, Tips and DoorNumber, using SAVE/RESTORE commands:

Code: Select all

#include "fivewin.ch"

MEMVAR Name, Birthday, Married, Tips, DoorNumber

function Main()

   SET DATE FORMAT TO "DD.MM.YYYY"

   if File( "MYVAR.MEM" )
      RESTORE FROM MYVAR ADDITIVE
      Tips := &Tips
   else
      PUBLIC   Name     := "Hakan", ;
               Birthday := {^ 1960/11/15 }, ;
               Married  := .F., ;
               Tips     := { "aaa", "bbb", "ccc", "ddd" }, ;
               DoorNumber := 456
   endif

   ? Name, Birthday, Married, DoorNumber

   // Use the variables
   EDITVARS Name,Birthday,Married,DoorNumber
   XBROWSER Tips FASTEDIT TITLE "Tips"

   // Finally Save
   Tips := FW_ValToExp( Tips )
   SAVE TO MYVAR ALL

return nil
 
The SAVE/RESTORE commands are very reliable and in most cases enough for saving and restoring variable names and values across sessions of the same appilcation as well as for communication between applications. A lot simpler than saving to DBF or others.

Before going to the next post, please do build and run this program. In the first run you will see the original values. Modify the values as you like. In the second run, you will see the modified values.

Image

If for some reason, you insist on saving to DBF but not to MEM files, please see the next post.

Re: An advice needed to save same variables and its value to mem

Posted: Wed Dec 30, 2020 2:06 pm
by nageswaragunupudi
Saving/Restoring variables and values to/from Memo Field of DBF

This sample saves the variable names and values to memofield of dbf and restores ane initiates the variables.

The functionality and behavior are identical to the previous sample.

Code: Select all

#include "fivewin.ch"

REQUEST DBFCDX

#xcommand SAVE VARS <v1>[,<vN>] TO FIELD <fld> => <fld> := \{ \{ <"v1">, <v1> \} [,\{ <"vN">, <vN> \}] \}
#xcommand RESTORE VARS FROM FIELD <fld> => AEval( <fld>, { |a | &( a\[ 1 \] ) := a\[ 2 \] } )

function Main()

   SET DATE FORMAT TO "DD.MM.YYYY"

   PUBLIC Name, Birthday, Married, Tips, DoorNumber

   if !File( "MYVARS.DBF" )
      DBCREATE( "MYVARS", {{ "MYVARS", "M", 8, 0}}, "DBFCDX", .T., "MYVARS" )
      DBAPPEND()
      FIELD->MYVARS := {{"Name","Hakan"}, ;
                        {"Birthday", {^ 1960/11/15 } }, ;
                        {"Married", .F.}, ;
                        {"Tips", {"aaa","bbb","ccc","ddd"}}, ;
                        {"DoorNumber", 456}}
      CLOSE MYVARS
   endif

   // Read and initialize variables from memofield
   USE MYVARS NEW SHARED READONLY VIA "DBFCDX"
   RESTORE VARS FROM FIELD MYVARS->MYVARS
   CLOSE MYVARS

   ? Name, Birthday, Married, DoorNumber

   // Use the variables
   EDITVARS Name,Birthday,Married,DoorNumber
   XBROWSER Tips FASTEDIT TITLE "Tips"

   // Finally Save to memofield
   USE MYVARS NEW SHARED VIA "DBFCDX"
   if MYVARS->( DbRLock() )
      SAVE VARS Name, Birthday, Married, Tips, DoorNumber to FIELD MYVARS->MYVARS
   endif
   CLOSE MYVARS

return nil
 

Re: An advice needed to save same variables and its value to mem

Posted: Wed Dec 30, 2020 5:22 pm
by Horizon
Thank you very much for full support.

I have solved my problem with yours great suport. This informations should be in wiki.