Using Hash in Harbour with bcc7

Using Hash in Harbour with bcc7

Postby byron.hopp » Wed Dec 16, 2015 8: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" )
// bhopp@matrixcomputer.com
MsgInfo( xJson["AuthToken"],"AuthToken" )
// abcdefghijklmnopqrstuvwxyz

MsgInfo( hb_hKeyAt(xJson,1) ,"UserName" )
// UserName
MsgInfo( hb_hKeyAt(xJson,2) ,"AuthToken" )
// AuthToken

MsgInfo( xJson:UserName ,"UserName" )
// generates error...
MsgInfo( xJson:AuthToken ,"AuthToken" )
// 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
byron.hopp
 
Posts: 382
Joined: Sun Nov 06, 2005 3:55 pm
Location: Southern California, USA

Re: Using Hash in Harbour with bcc7

Postby byron.hopp » Wed Dec 16, 2015 8: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
byron.hopp
 
Posts: 382
Joined: Sun Nov 06, 2005 3:55 pm
Location: Southern California, USA

Re: Using Hash in Harbour with bcc7

Postby nageswaragunupudi » Wed Dec 16, 2015 9: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
User avatar
nageswaragunupudi
 
Posts: 10620
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: Using Hash in Harbour with bcc7

Postby toninhofwi » Wed Dec 16, 2015 9: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.
toninhofwi
 
Posts: 172
Joined: Tue Oct 18, 2005 10:01 am

Re: Using Hash in Harbour with bcc7

Postby nageswaragunupudi » Wed Dec 16, 2015 9: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
User avatar
nageswaragunupudi
 
Posts: 10620
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: Using Hash in Harbour with bcc7

Postby Maurizio » Thu Dec 17, 2015 8: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
User avatar
Maurizio
 
Posts: 824
Joined: Mon Oct 10, 2005 1:29 pm

Re: Using Hash in Harbour with bcc7

Postby byron.hopp » Thu Dec 17, 2015 9: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
byron.hopp
 
Posts: 382
Joined: Sun Nov 06, 2005 3:55 pm
Location: Southern California, USA

Re: Using Hash in Harbour with bcc7

Postby hua » Fri Dec 18, 2015 3:54 am

Byron, Maurizio post already answer your question. Just simply add
Code: Select all  Expand view
#include "xhb.ch"
and link-in xhb.lib
FWH 11.08/FWH 19.12
BCC5.82/BCC7.3
xHarbour/Harbour
hua
 
Posts: 1072
Joined: Fri Oct 28, 2005 2:27 am

Re: Using Hash in Harbour with bcc7

Postby byron.hopp » Fri Dec 18, 2015 6: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
byron.hopp
 
Posts: 382
Joined: Sun Nov 06, 2005 3:55 pm
Location: Southern California, USA

Re: Using Hash in Harbour with bcc7

Postby byron.hopp » Fri Dec 18, 2015 6:15 pm

Worked, Thanks again...
Code: Select all  Expand view

#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
byron.hopp
 
Posts: 382
Joined: Sun Nov 06, 2005 3:55 pm
Location: Southern California, USA


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 92 guests