AADD() equivalent for HASH

629 views
Skip to first unread message

Shaji Thomas

unread,
Jun 19, 2020, 3:10:20 AM6/19/20
to Harbour Users
Hi,

In advance I am disclosing my inexperience  in harbour hash table manipulation.. 

hList        := { {  "Name "  =>   "Alex",  "Age"  =>  35 } , { "Name "  =>   "Robert",  "Age"  =>  45 } }

hTmp      :=  { "Name "  =>   "NewMan",  "Age"  =>  80 }

hFinal      := {  {  "Name "  =>   "Alex",  "Age"  =>  35 }       ,;
                       {  "Name "  =>   "Robert",  "Age"  =>  45 }   ,;
                       {  "Name "  =>   "Alex",  "Age"  =>  35 }  }
                      
What is the correct method  for adding hTmp into hLsit  (result -> hFinal). I have done some adjustments with aadd() but I am looking for the HASH equivalent.

Shaji Thomas






Shaji Thomas

unread,
Jun 19, 2020, 3:14:03 AM6/19/20
to Harbour Users

Please replace the hFinal as :-

hFinal      := {  {  "Name "  =>   "Alex",  "Age"  =>  35 }       ,;
                       {  "Name "  =>   "Robert",  "Age"  =>  45 }   ,;
                       {  "Name "  =>   "NewMan",  "Age"  =>  80 }  }

wanst...@gmail.com

unread,
Jun 19, 2020, 7:45:47 AM6/19/20
to Harbour Users
From the nice xHarbour Manual:

Hash()

Creates a new hash.


Syntax

Hash( [<xKey1>, <xValue1> [, <xKeyN>, <xValueN>] ] ) --> hHash

Arguments

<xKey1> .. <xKeyN>
<xKey1> is the key value associated with <xValue1> in the hash. The key value is usually of data type Character, but may be a Date or Numeric value.
<xValue1> .. <xValueN>
<xValue1> is a value in the hash which can be accessed through its key value <xKey1>.

Return

The function returns a new hash, populated with the specified key/value pairs. If no parameter is passed, an empty hash is returned.

If the function is called with an odd number of arguments, or key values are specified which are not of data type Character, Date or Numeric, the return value is NIL.


Description

Hash variables are usually initialized within a variable declaration using the literal Hash operator {=>}. The Hash() function is equivalent to this operator and allows for creating hashes programmatically outside a variable declaration.

Hashes can be populated with key/value pairs by passing an even number of parameters to the function. Data representing the key values must be of orderable data types which restricts them to the data types C, D and N.


Info

See also: {=>}, HAllocate(), HClone(), HCopy(), HGet(), HDel(), HEval(), HSet(), HSetCaseMatch(), HGetPartition()
Category: Hash functions , xHarbour extensions
Source: vm\hash.c
LIB: xhb.lib
DLL: xhbdll.dll


Example

// The example creates hashes using the literal hash operator
// and the Hash() function.

   PROCEDURE Main
      LOCAL hHash1 := { "OPT1" => 10, "OPT2" => 20 }
      LOCAL hHash2, hHash3

      hHash2 := Hash( "OPT2", 200, "OPT3", 300, "OPT4", 400 )

      hHash3 := hHash1 + hHash2

      ? ValToPrg( hHash3 )
      // { "OPT1" => 10, "OPT2" => 200, "OPT3" => 300, "OPT4" => 400 }
   RETURN

Pete

unread,
Jun 19, 2020, 7:46:59 AM6/19/20
to Harbour Users
Hi,

Your <hList> is NOT a hash table. it is an array (you'd better name it <aList>.
So, your question is transcribed to something like: "how do I add an element in an array?"
obvious the answer is: AAdd(hList , hTmp )

regards,
Pete

Shaji Thomas

unread,
Jun 19, 2020, 8:05:33 AM6/19/20
to Harbour Users


Thank you Pete


aList    := { {  "Name "  =>   "Alex",  "Age"  =>  35 } , { "Name "  =>   "Robert",  "Age"  =>  45 } }

How can I place these items in a hash  table (if it is  possible)

Shaji Thomas






oleksa

unread,
Jun 19, 2020, 8:06:15 AM6/19/20
to harbou...@googlegroups.com
Hi!

I suggest to use next code:

func main

hList := { "Alex" => { "Age" => 35 } , "Robert" => { "Age" => 45 } }

hTmp :=  { "NewMan" => { "Age" => 80 } }

hb_HMerge( hList, hTmp )

return

Regards,
Oleksii Myronenko


19 червня 2020, 10:10:25, від "Shaji Thomas" <shaj...@gmail.com>:

--
--
You received this message because you are subscribed to the Google
Groups "Harbour Users" group.
Unsubscribe: harbour-user...@googlegroups.com
Web: http://groups.google.com/group/harbour-users

---
You received this message because you are subscribed to the Google Groups "Harbour Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to harbour-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/harbour-users/5b710539-7ade-4018-971c-601fbb329ec5o%40googlegroups.com.

Shaji Thomas

unread,
Jun 19, 2020, 8:58:47 AM6/19/20
to Harbour Users

Hi friend, 

Thank you for your reply

but my requirement was to create a NESTED structure for using with hb_jsonencode()

Shaji Thomas

 

oleksa

unread,
Jun 19, 2020, 9:12:58 AM6/19/20
to harbou...@googlegroups.com
hList, hTmp, hFinal is an array that contains a hash, and AAdd() help you here.

Regards,
Oleksii Myronenko

19 червня 2020, 15:58:52, від "Shaji Thomas" <shaj...@gmail.com>:

--
--
You received this message because you are subscribed to the Google
Groups "Harbour Users" group.
Unsubscribe: harbour-user...@googlegroups.com
Web: http://groups.google.com/group/harbour-users

---
You received this message because you are subscribed to the Google Groups "Harbour Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to harbour-user...@googlegroups.com.

Luigi Ferraris

unread,
Jun 24, 2020, 11:13:52 AM6/24/20
to Harbour Users

Il giorno venerdì 19 giugno 2020 14:58:47 UTC+2, Shaji Thomas ha scritto:

Hi friend, 

but my requirement was to create a NESTED structure for using with hb_jsonencode()



Hi Shaji I hope next code can help you

   LOCAL hMain, hFamily, hWho

   hMain := {=>}

   hFamily := {=>}
   hWho := {=>}
   hWho["money"] := 35
   hb_Hset( hFamily, "Emy", hWho )   // hFamily["Emy"] := hWho

   hWho := {=>}
   hWho["money"] := 25
   hb_Hset( hFamily, "Evy", hWho )   // hFamily["Evy"] := hWho

   hWho := {=>}
   hWho["money"] := 15
   hb_Hset( hFamily, "Ely", hWho )   // hFamily["Ely"] := hWho
  
   hb_Hset( hMain, "female", hFamily )   // hMain["female"] := hFamily


   hFamily := {=>}
   hWho := {=>}
   hWho["money"] := 10
   hb_Hset( hFamily, "Huey", hWho )   // hFamily["Huey"] := hWho

   hWho := {=>}
   hWho["money"] := 5
   hb_Hset( hFamily, "Dewey", hWho )   // hFamily["Dewey"] := hWho

   hWho := {=>}
   hWho["money"] := 0
   hb_Hset( hFamily, "Louie", hWho )   // hFamily["Louie"] := hWho
  
   hb_Hset( hMain, "male", hFamily )   // hMain["male"] := hFamily
  
   hb_MemoWrit( "list", hb_JsonEncode( hMain, 3, /*cCodePage )*/ ) )


Best regards
Luigi Ferraris

José Quintas

unread,
Jun 24, 2020, 11:52:32 AM6/24/20
to harbou...@googlegroups.com

Only another way to do the same:

PROCEDURE Main

   LOCAL a, b, c

   WITH OBJECT a := hb_Hash()
      WITH OBJECT b := a[ "female" ] := hb_Hash()
         WITH OBJECT c := b[ "Emy" ] := hb_Hash()
            c[ "money" ] := 25
         ENDWITH
         WITH OBJECT c := b[ "Evy" ] := hb_Hash()
            c[ "money" ] := 25
         ENDWITH
         WITH OBJECT c := b[ "Ely" ] := hb_Hash()
            c[ "money" ] := 15
         ENDWITH
      ENDWITH
      WITH OBJECT b := a[ "male" ] := hb_Hash()
         WITH OBJECT c := b[ "Huey" ] := hb_Hash()
            c[ "money" ] := 10
         ENDWITH
         WITH OBJECT c := b[ "Dewey" ] := hb_Hash()
            c[ "money" ] := 5
         ENDWITH
         WITH OBJECT c := b[ "Louie" ] := hb_Hash()
            c[ "money" ] := 0
         ENDWITH
      ENDWITH
   ENDWITH
   hb_MemoWrit( "list2", hb_JsonEncode( a, 3 ) )
  
   RETURN


{
   "female":
   {
      "Emy":
      {
         "money": 25
      },
      "Evy":
      {
         "money": 25
      },
      "Ely":
      {
         "money": 15
      }
   },
   "male":
   {
      "Huey":
      {
         "money": 10
      },
      "Dewey":
      {
         "money": 5
      },
      "Louie":
      {
         "money": 0
      }
   }
}


José M. C. Quintas

--
--
You received this message because you are subscribed to the Google
Groups "Harbour Users" group.
Unsubscribe: harbour-user...@googlegroups.com
Web: http://groups.google.com/group/harbour-users

---
You received this message because you are subscribed to the Google Groups "Harbour Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to harbour-user...@googlegroups.com.

Shaji Thomas

unread,
Jun 25, 2020, 4:07:31 AM6/25/20
to Harbour Users

Thank you, I will work on it..

Klas Engwall

unread,
Jun 25, 2020, 7:23:30 AM6/25/20
to harbou...@googlegroups.com
Hi Shaji Thomas,

Reading the entire thread in sequence I get the feeling that it has
taken off in a different direction than you had originally intended,
especially the suggestions by Luigi and José. I can imagine their
solutions being appropriate for something like a highschool class
catalog where the people will be the same forever and any duplicate
names can be solved by using "JohnA" and "JohnB" etc to distinguish the
records. Then you can ask "What about JohnA" and easily extract JohnA's
record.

But for a catalog of customers, for example, an array of customer hashes
would be more suitable than a separate hash for each person within the
main hash. Using the person's name as a label - that is as the name of
the individual hash - does not seem like a good idea for this kind of
use. Normally a json file would be processed sequentially, not used for
random access to individual records. And it seems you originally wanted
to create an array in the main hash but did not know the syntax for that.

Using function syntax (but operator syntax works too) it would look
something like this:

aPersons := {}
hPerson := hb_hash()
hPerson[ "name" ] := "Alex"
hPerson[ "age" ] := 35
aadd( aPersons, hPerson )

Then loop back to line 2 until finished

Finally:
hMainHash := hb_hash()
hMainHash[ "persons" ] := aPersons

Each <hPerson> hash will be "flat" like a dbf table with the labels
("name", "age" etc) corresponding to the field names in the table and
the values ("Alex", 35 etc) corresponding to the data in the fields. The
json file will be easy to read and understand.

Of course, the structure of your hash must match the structure of the
json file according to the specification. Is this something you are
creating for internal use or is there an external specification that you
have to follow? In the latter case, your only option is of course to
follow the specs exactly.

You may also want to look at the hb_hkeeporder() function to make sure
the elements in the person records in the json file are not rearranged
in a different order but will always appear in the order you set from
the beginning. All the hb_h*() functions can be found at Pete's site:
https://github.com/Petewg/harbour-core/wiki

Regards,
Klas

Shaji Thomas

unread,
Jun 26, 2020, 1:54:54 AM6/26/20
to Harbour Users

Thank you Klass. In fact I was expecting your reply.. 

my requirement was to create a json from a nested hash(array of hashes) to  upload into a tax website.
I managed that with aadd() but my confusion was about any hb_h??? function is there for this purpose..
Now I am clear that as pete & you wrote it is an array with hash elements, so I can use aadd() without any doubt.

Thank you so much..

Shaji Thomas


Reply all
Reply to author
Forward
0 new messages