HASH vs single variables

User avatar
Otto
Posts: 6401
Joined: Fri Oct 07, 2005 7:07 pm
Has thanked: 12 times
Been thanked: 2 times
Contact:

HASH vs single variables

Post by Otto »

Hello friends,

I am now trying an approach to use a HASH instead of individual variables.
So far, I have named the HASH hApp.

But I think the source code is easier to read if you just use h[] as the name.
You can then see the HashKey better.

First, I define a hash.
Then, when I need a variable somewhere, I insert a new key to be able to access it throughout the program.

An xBrowse(h) shows all keys - variables - used in the program with their current value.

This function shows all the "variables" used, the keys and values.

Best regards,
Otto



Image

Image
********************************************************************
mod harbour - Vamos a la conquista de la Web
modharbour.org
https://www.facebook.com/groups/modharbour.club
********************************************************************
User avatar
Marc Venken
Posts: 1483
Joined: Tue Jun 14, 2016 7:51 am
Location: Belgium

Re: HASH vs single variables

Post by Marc Venken »

I also think like you do. Hash before many variables.
Marc Venken
Using: FWH 23.08 with Harbour
AntoninoP
Posts: 375
Joined: Tue Feb 10, 2015 9:48 am
Location: Albenga, Italy
Contact:

Re: HASH vs single variables

Post by AntoninoP »

good.. next step use classes
paquitohm
Posts: 287
Joined: Fri Jan 14, 2022 8:37 am
Been thanked: 1 time

Re: HASH vs single variables

Post by paquitohm »

Hola,

Usar hashes en vez de variables TIENE UN PROBLEMA.
El compilador es incapaz de detectar si te has equivocado al teclear y entonces crea un item nuevo.

Con hash:

Code: Select all | Expand

Local h:= {=>}
h["CustomarCode"]:= "0001"    // No error
 
Con variable:

Code: Select all | Expand

Local cCustomerCode
cCustomarCode:= "0001"       // Error in compilation time
 

Con hash, sistema mejorado:

Code: Select all | Expand

Local h:= {"cCustomerCode"=>NIL}
hb_HAutoAdd(h, .f.)  // Swith off auto declare variables
h["CustomarCode"]:= "0001"    // Run-time error
 
Regards
User avatar
Otto
Posts: 6401
Joined: Fri Oct 07, 2005 7:07 pm
Has thanked: 12 times
Been thanked: 2 times
Contact:

Re: HASH vs single variables

Post by Otto »

Hello Antonino,
Can you maybe list some pros and cons here.
What advantage would a class have here?

I use classes for e.g. program setup, etc.. with my FIVEWIN programming.
But it seems to me that with mod harbour the OOP is an overhead that brings nothing. Therefore a HASH seems favorable to me here.
Probably this is because at the moment the programs are still very simple.

Best regards,
Otto
********************************************************************
mod harbour - Vamos a la conquista de la Web
modharbour.org
https://www.facebook.com/groups/modharbour.club
********************************************************************
User avatar
Otto
Posts: 6401
Joined: Fri Oct 07, 2005 7:07 pm
Has thanked: 12 times
Been thanked: 2 times
Contact:

Re: HASH vs single variables

Post by Otto »

Paquitohm, you are right. You have to be more precise here.
Having used this method primarily with mod harbour, I would like to highlight one benefit that I see.

I pass the HASH to a Javascript object and then have practically the same variable names in the Harbor code and in the HTML/JS code.


Best regards,
Otto
********************************************************************
mod harbour - Vamos a la conquista de la Web
modharbour.org
https://www.facebook.com/groups/modharbour.club
********************************************************************
User avatar
VictorCasajuana
Posts: 269
Joined: Wed Mar 28, 2018 4:38 pm
Location: Vinaròs
Has thanked: 1 time
Contact:

Re: HASH vs single variables

Post by VictorCasajuana »

Otto wrote:Hello Antonino,
Can you maybe list some pros and cons here.
What advantage would a class have here?

I use classes for e.g. program setup, etc.. with my FIVEWIN programming.
But it seems to me that with mod harbour the OOP is an overhead that brings nothing. Therefore a HASH seems favorable to me here.
Probably this is because at the moment the programs are still very simple.

Best regards,
Otto
OOP always contributes.
You may have a small program now, but if you do it with OOP (classes and objects) when it grows you will have a good foundation.

When necessary, you can give a logic to the assignment of a value ( setters and getters ). Another advantage is with the editor, if you use classes, it helps you when writing the code since it autocompletes the properties
--------
¿ Y porque no ?
¿ And why not ?
User avatar
Otto
Posts: 6401
Joined: Fri Oct 07, 2005 7:07 pm
Has thanked: 12 times
Been thanked: 2 times
Contact:

Re: HASH vs single variables

Post by Otto »

Victor, assume that I know both types of programming.

Using a hash table instead of classes can have its own advantages as well.

Hash tables are simpler and more lightweight, and can be faster for certain types of operations, such as looking up an item by key. They also don't require the overhead of creating and managing objects, which can be beneficial for smaller programs or programs that need to handle a large number of simple data structures.

I think I will do many more tests and then develop a personal style.

But at this stage, I'm very excited about the possibilities that HASHs offer.

Best regards
Otto
********************************************************************
mod harbour - Vamos a la conquista de la Web
modharbour.org
https://www.facebook.com/groups/modharbour.club
********************************************************************
User avatar
Otto
Posts: 6401
Joined: Fri Oct 07, 2005 7:07 pm
Has thanked: 12 times
Been thanked: 2 times
Contact:

Re: HASH vs single variables

Post by Otto »

Hello friends,

I am in the process of revising WINHOTEL a bit.
On this occasion I now change the use of variables to hashes.

Here is an example of how I make a switch row with very minimal definition.

Only the red framed part is to be defined for each button.
Best regards,
Otto

Image

Image

Image

Image
********************************************************************
mod harbour - Vamos a la conquista de la Web
modharbour.org
https://www.facebook.com/groups/modharbour.club
********************************************************************
User avatar
Otto
Posts: 6401
Joined: Fri Oct 07, 2005 7:07 pm
Has thanked: 12 times
Been thanked: 2 times
Contact:

Re: HASH vs single variables

Post by Otto »

Dear friends,
Now we have done some more styling.
I think it looks more up-to-date.
Best regards,
Otto

Image

Image

Image
********************************************************************
mod harbour - Vamos a la conquista de la Web
modharbour.org
https://www.facebook.com/groups/modharbour.club
********************************************************************
User avatar
Otto
Posts: 6401
Joined: Fri Oct 07, 2005 7:07 pm
Has thanked: 12 times
Been thanked: 2 times
Contact:

Re: HASH vs single variables

Post by Otto »

Hello friends,
I have been using a static hash variable for some time now to partially replace local variables. It somehow feels safer to me when making changes to old source code.

I don't need to declare the variables. I simply assign a value to the key, and then I have access to it throughout the prg.

Xbrowse(h)
Lists, for example, all the "variables" used with their values.


Best regards,
Otto
********************************************************************
mod harbour - Vamos a la conquista de la Web
modharbour.org
https://www.facebook.com/groups/modharbour.club
********************************************************************
User avatar
Marc Venken
Posts: 1483
Joined: Tue Jun 14, 2016 7:51 am
Location: Belgium

Re: HASH vs single variables

Post by Marc Venken »

I also use this technique. Very easy and better overview. I use a hot-key that pops a xbrowse where i can add a variable on the fly for using in my program.
It also pops all var's with there current values and that is happy for info/debug.
and I can have labels (that gives the type and description of content) that afther time I forgot with older var's
Passing, declaring and remember the names are gone )))
Marc Venken
Using: FWH 23.08 with Harbour
User avatar
Otto
Posts: 6401
Joined: Fri Oct 07, 2005 7:07 pm
Has thanked: 12 times
Been thanked: 2 times
Contact:

Re: HASH vs single variables

Post by Otto »

Marc, very interesting. I will assign a hotkey too. Could you provide an example of having labels that give the type and description of content?

Best regards,
Otto
********************************************************************
mod harbour - Vamos a la conquista de la Web
modharbour.org
https://www.facebook.com/groups/modharbour.club
********************************************************************
User avatar
Marc Venken
Posts: 1483
Joined: Tue Jun 14, 2016 7:51 am
Location: Belgium

Re: HASH vs single variables

Post by Marc Venken »

Otto,

Label is more ment as info (maybe you think a popup label of a king)

In the popup I can change/add.
Type = Return Type data
Veld = VarName
Data = content of Var (can me array,...)
DataType = type that the data is made in (ex. Datatype = array, but Type = C as return value for program
info = what's about...to remember afther time )))
trim = Should result be trimmend.

All tips, enhancements are welcome...

the small code (but you have this already) just for forum

Code: Select all | Expand


setkey( VK_F6,{|| xbrowser("system") FASTEDIT })  //  HotKey for HASH

hash_setup() // Setup alle datagegevens in een hash

function hash_setup()
   netopen("system")
   do while !system->(eof())
     cVeld:=alltrim(system->veld)
     cData:=alltrim(system->data)
     do case
      case system->data_type = "A"
        if !empty(cData)
           h_System["&cVeld"]  := strtoArr(cData)
        else
           h_System["&cVeld"]  := {}
        endif
      case system->data_type = "N"
        h_System["&cVeld"]  := val(cData)
      case system->data_type = "L"
        if cData = ".T."
           h_System["&cVeld"]  := .T.
        else
           h_System["&cVeld"]  := .F.
        endif
      otherwise
        h_System["&cVeld"]  := if(system->trim,alltrim(system->data),system->data)
     endcase
     system->(dbskip())
   enddo
   close system
return NIL



 

Image
Marc Venken
Using: FWH 23.08 with Harbour
User avatar
Otto
Posts: 6401
Joined: Fri Oct 07, 2005 7:07 pm
Has thanked: 12 times
Been thanked: 2 times
Contact:

Re: HASH vs single variables

Post by Otto »

Marc,
Do I understand the code correctly?

The `xbrowse` you're showing is from the DBF file, not the hash, right?

Best regards,
Otto
********************************************************************
mod harbour - Vamos a la conquista de la Web
modharbour.org
https://www.facebook.com/groups/modharbour.club
********************************************************************
Post Reply