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

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

Postby Horizon » Tue Dec 29, 2020 1:19 pm

Hi,

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

Code: Select all  Expand view
{{"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.04
Horizon
 
Posts: 1322
Joined: Fri May 23, 2008 1:33 pm

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

Postby cnavarro » Tue Dec 29, 2020 2:40 pm

Code: Select all  Expand view

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
User avatar
cnavarro
 
Posts: 6541
Joined: Wed Feb 15, 2012 8:25 pm
Location: España

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

Postby Antonio Linares » Tue Dec 29, 2020 4:58 pm

field->memo := hb_serialize( aVars )

...

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

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42077
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

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

Postby Horizon » Wed Dec 30, 2020 5:55 am

Thank you Mr. Navarro, Antonio,

I will look it.
Regards,

Hakan ONEMLI

Harbour & MSVC 2022 & FWH 23.04
Horizon
 
Posts: 1322
Joined: Fri May 23, 2008 1:33 pm

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

Postby nageswaragunupudi » Wed Dec 30, 2020 2: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: Select all  Expand view

FIELD->MEMOFIELD := aVars
 


Later, we can read the data into array like this
Code: Select all  Expand view

aVars := FIELD->MEMOFIELD
 


You can build and run this program to test this:
Code: Select all  Expand view

#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: 10622
Joined: Sun Nov 19, 2006 5:22 am
Location: India

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

Postby nageswaragunupudi » Wed Dec 30, 2020 2: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: Select all  Expand view

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  Expand view

#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: 10622
Joined: Sun Nov 19, 2006 5:22 am
Location: India

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

Postby nageswaragunupudi » Wed Dec 30, 2020 2: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: Select all  Expand view

#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
User avatar
nageswaragunupudi
 
Posts: 10622
Joined: Sun Nov 19, 2006 5:22 am
Location: India

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

Postby Horizon » Wed Dec 30, 2020 5: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.04
Horizon
 
Posts: 1322
Joined: Fri May 23, 2008 1:33 pm


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 55 guests