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

Post Reply
Horizon
Posts: 997
Joined: Fri May 23, 2008 1:33 pm

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

Post 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.
Regards,

Hakan ONEMLI

Harbour & VS 2019 & FWH 20.12
User avatar
cnavarro
Posts: 5792
Joined: Wed Feb 15, 2012 8:25 pm
Location: España

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

Post 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
C. Navarro
Hay dos tipos de personas: las que te hacen perder el tiempo y las que te hacen perder la noción del tiempo
Si alguien te dice que algo no se puede hacer, recuerda que esta hablando de sus limitaciones, no de las tuyas.
User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

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

Post by Antonio Linares »

field->memo := hb_serialize( aVars )

...

aVars := hb_deserialize( field->memo )
regards, saludos

Antonio Linares
www.fivetechsoft.com
Horizon
Posts: 997
Joined: Fri May 23, 2008 1:33 pm

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

Post by Horizon »

Thank you Mr. Navarro, Antonio,

I will look it.
Regards,

Hakan ONEMLI

Harbour & VS 2019 & FWH 20.12
User avatar
nageswaragunupudi
Posts: 8017
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

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

Post 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.
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
Posts: 8017
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

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

Post 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.
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
Posts: 8017
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

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

Post 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
 
Regards

G. N. Rao.
Hyderabad, India
Horizon
Posts: 997
Joined: Fri May 23, 2008 1:33 pm

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

Post by Horizon »

Thank you very much for full support.

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

Hakan ONEMLI

Harbour & VS 2019 & FWH 20.12
Post Reply