FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour An advice needed to save same variables and its value to mem
Posts: 1387
Joined: Fri May 23, 2008 01:33 PM
An advice needed to save same variables and its value to mem
Posted: Tue Dec 29, 2020 01:19 PM
Hi,

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

Code (fw): Select all Collapse
{{"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 & MSVC 2022 & FWH 23.06
Posts: 6755
Joined: Wed Feb 15, 2012 08:25 PM
Re: An advice needed to save same variables and its value to mem
Posted: Tue Dec 29, 2020 02:40 PM
Code (fw): Select all Collapse
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
Cristobal Navarro

Hay dos tipos de personas: las que te hacen perder el tiempo y las que te hacen perder la noción del tiempo

El secreto de la felicidad no está en hacer lo que te gusta, sino en que te guste lo que haces
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: An advice needed to save same variables and its value to mem
Posted: Tue Dec 29, 2020 04:58 PM

field->memo := hb_serialize( aVars )

...

aVars := hb_deserialize( field->memo )

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 1387
Joined: Fri May 23, 2008 01:33 PM
Re: An advice needed to save same variables and its value to mem
Posted: Wed Dec 30, 2020 05:55 AM

Thank you Mr. Navarro, Antonio,

I will look it.

Regards,



Hakan ONEMLI



Harbour & MSVC 2022 & FWH 23.06
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: An advice needed to save same variables and its value to mem
Posted: Wed Dec 30, 2020 02:02 PM
Saving and restoring arrays to and from MemoFields of DBF

Using DBFCDX, we can save an array in a memo field like this:
Code (fw): Select all Collapse
FIELD->MEMOFIELD := aVars


Later, we can read the data into array like this
Code (fw): Select all Collapse
aVars := FIELD->MEMOFIELD


You can build and run this program to test this:
Code (fw): Select all Collapse
#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




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
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: An advice needed to save same variables and its value to mem
Posted: Wed Dec 30, 2020 02:04 PM
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 (fw): Select all Collapse
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 (fw): Select all Collapse
#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.



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
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: An advice needed to save same variables and its value to mem
Posted: Wed Dec 30, 2020 02:06 PM
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 (fw): Select all Collapse
#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
Posts: 1387
Joined: Fri May 23, 2008 01:33 PM
Re: An advice needed to save same variables and its value to mem
Posted: Wed Dec 30, 2020 05:22 PM

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 & MSVC 2022 & FWH 23.06

Continue the discussion