FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Using Hash in Harbour with bcc7
Posts: 408
Joined: Sun Nov 06, 2005 03:55 PM
Using Hash in Harbour with bcc7
Posted: Wed Dec 16, 2015 08:14 PM

I am new to Harbour with bcc7. Have been using the commercial xHarbour for many years.

Is there a difference in using Hash variables between these two languages?

In the code below, if I attempt to call the hash variable directly like an object:

xJson:UserName my app errors out (no exported method), but xJson["UserName"] seems to work o.k.

Am I using the first option above incorrectly?

function Main()
Local hJson := GetEmptyHash()
Local cJson := ""
Local xJson := nil
Local nSymbols := 0

hJson["UserName"]  := "bhopp@matrixcomputer.com"
hJson["AuthToken"] := "anbdefghijklmnopqustuvwxyz"
cJson := hb_JsonEncode( hJson,.f. )

MsgInfo( cJson,"Json.Stringify" )
// {"UserName" :"bhopp@matrixcomputer.com"}

nSymbols := hb_JsonDecode( cJson,@xJson )
MsgInfo( Str( nSymbols ),"Number of Symbols processed." )
// nSymbols == 80

MsgInfo( xJson["UserName"] ,"UserName"  )
// <!-- e --><a href="mailto:bhopp@matrixcomputer.com">bhopp@matrixcomputer.com</a><!-- e -->
MsgInfo( xJson[&quot;AuthToken&quot;],&quot;AuthToken&quot; )
// abcdefghijklmnopqrstuvwxyz

MsgInfo( hb_hKeyAt(xJson,1) ,&quot;UserName&quot;  )
// UserName
MsgInfo( hb_hKeyAt(xJson,2) ,&quot;AuthToken&quot; )
// AuthToken

MsgInfo( xJson:UserName ,&quot;UserName&quot;  )
// generates error...
MsgInfo( xJson:AuthToken ,&quot;AuthToken&quot; )
// never gets here...

return nil

Function GetEmptyHash()
Local hHash := Hash()
HSetAACompatibility( hHash, .T. )
HSetCaseMatch( hHash, .F. )
Return hHash

Byron ...

Thanks,

Byron Hopp

Matrix Computer Services
Posts: 408
Joined: Sun Nov 06, 2005 03:55 PM
Re: Using Hash in Harbour with bcc7
Posted: Wed Dec 16, 2015 08:16 PM

Correction...

MsgInfo( cJson,"Json.Stringify" )
// {"UserName" :"bhopp@matrixcomputer.com"}
really returns:
// {"UserName" :"bhopp@matrixcomputer.com","AuthToken" :"abcdefghijklmnopqrstuvwxyz"}

Thanks,

Byron Hopp

Matrix Computer Services
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: Using Hash in Harbour with bcc7
Posted: Wed Dec 16, 2015 09:31 PM

In xHarbour both

hVar:Key and hVar["Key"] work

In Harbour
only hVar["Key"] works
hVar:Key does not work

Regards



G. N. Rao.

Hyderabad, India
Posts: 182
Joined: Tue Oct 18, 2005 10:01 AM
Re: Using Hash in Harbour with bcc7
Posted: Wed Dec 16, 2015 09:52 PM
nageswaragunupudi wrote:In xHarbour both

hVar:Key and hVar["Key"] work

In Harbour
only hVar["Key"] works
hVar:Key does not work


Hi.

Works in harbour but you need recompile Harbour with -DHB_HASH_MSG_ITEMS

Regards.
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: Using Hash in Harbour with bcc7
Posted: Wed Dec 16, 2015 09:55 PM
Works in harbour but you need recompile Harbour with -DHB_HASH_MSG_ITEMS

Thanks for the information. I did not know.
Regards



G. N. Rao.

Hyderabad, India
Posts: 842
Joined: Mon Oct 10, 2005 01:29 PM
Re: Using Hash in Harbour with bcc7
Posted: Thu Dec 17, 2015 08:08 AM

From C:\harbour\doc\xhb-diff.txt
  OOP INTERFACE TO HASH ARRAYS    ###
==========================================
xHarbour allows to access items in hash array using OOP interface.
hVal[ "ABC" ] := 100 can be alternatively written as hVal:ABC := 100.
Using OOP interface is slower then [] operator but it works for all
indexes which are valid upper case [x]Harbour identifiers.
By default Harbour core code does not give such functionality but
it has strong enough OOP API to allow adding such extension without
touching core code even by user at .prg level. It was implemented
in Harbour in XHB.LIB.
This code can be compiled and executed by both compilers:

#ifndef XHARBOUR
         #include "xhb.ch"
      #endif
      proc main()
         local hVal := {=>}
         hVal["ABC"] := 100
         hVal:QWE := 200
         hVal:ZXC := 300
         hVal:QWE += 50
         ? hVal:ABC, hVal:QWE, hVal:ZXC
         ? hVal["ABC"], hVal["QWE"], hVal["ZXC"]
      return

Some Harbour users used to compile Harbour core code with HB_HASH_MSG_ITEMS
macro which enables such functionality directly in core code but it's not
necessary with current code and it exists for historical reasons.
It's possible that in the future support for above macro will be removed
or it will be replaced by runtime switch which can be enabled/disabled for
each hash array separately.

Maurizio

http://www.nipeservice.com

Posts: 408
Joined: Sun Nov 06, 2005 03:55 PM
Re: Using Hash in Harbour with bcc7
Posted: Thu Dec 17, 2015 09:32 PM

Do I need to recompile Harbour to get this option, or is there a way to flip a switch with the latest version (downloaded Sunday).

If I do need to recompile Harbour, should I have what is needed to compile it, or do I need to download something additional from the site to recompile, and is there a batch file to perform this recompile?

Thanks,

Byron ...

Thanks,

Byron Hopp

Matrix Computer Services
Posts: 1096
Joined: Fri Oct 28, 2005 02:27 AM
Re: Using Hash in Harbour with bcc7
Posted: Fri Dec 18, 2015 03:54 AM
Byron, Maurizio post already answer your question. Just simply add
Code (fw): Select all Collapse
#include "xhb.ch"
and link-in xhb.lib
FWH 11.08/FWH 19.12

BCC5.82/BCC7.3

xHarbour/Harbour
Posts: 408
Joined: Sun Nov 06, 2005 03:55 PM
Re: Using Hash in Harbour with bcc7
Posted: Fri Dec 18, 2015 06:52 AM

My Apologies, I wasn't able to find xHb.ch in my include folder, but I downloaded the Source and it was there.

Seems like I have 3.0.0, I hope this is new enough.

I will give it a try.

Thank you both.

Byron ...

Thanks,

Byron Hopp

Matrix Computer Services
Posts: 408
Joined: Sun Nov 06, 2005 03:55 PM
Re: Using Hash in Harbour with bcc7
Posted: Fri Dec 18, 2015 06:15 PM

Worked, Thanks again...

#include "FiveWin.ch"

#ifndef __XHARBOUR__
   #include "xHb.ch"
#endif

Function Main()
    Local hJson    := Hash()
    Local cJson    := ""
    Local xJson    := nil
    Local nSymbols := 0

    HSetAACompatibility( hJson, .T. )
   HSetCaseMatch(       hJson, .F. )

    hJson["UserName"]  := "bhopp@matrixcomputer.com"
    hJson["AuthToken"] := "anbdefghijklmnopqustuvwxyz"

    MsgInfo( hJson:UserName, "UserName" )
    MsgInfo( hJson:AuthToken,"AuthToken" )
Return nil
Thanks,

Byron Hopp

Matrix Computer Services

Continue the discussion