Associative arrays in harbour and xharbour

Associative arrays in harbour and xharbour

Postby Franklin Demont » Sun Sep 30, 2012 6:42 am

Hello,

Working on the conversion from XHB to HBR , i found some differences between harbour and xharbour, but also probably a wrong define in hbcombat.ch

A associative array is a hash array where HSETAACOMPATIBILITY is set to .T. in xharbour , hb_KeepOrder in harbour

Func TAssociativeArray()
LOCAL h := Hash()
HSETAACOMPATIBILITY(h,.T.) // translated in hb_hKeepOrder(h,.T.) (harbour)
RETURN h

The define in hbCombat.ch :

#xtranslate HSETAACOMPATIBILITY([<x,...>]) => {| h | ;;
hb_HKEEPORDER( h ) ;;
return .T. ; }:eval( <x> )

Seems not to work , i changed it in :

#xtranslate HSETAACOMPATIBILITY(<x>,<y>) => hb_HKEEPORDER(<x>,<y>)

Besides that xharbour has a different behaviour. Creation order can be retrieved as :

FOR i := 1 TO LEN(h) ; ? h[i] ; NEXT // We can use numeric index

In harbour we become the creation order with :

FOR EACH cKey IN h ; ? h[cKey] ; NEXT // In xharbour this gives alfabetic order on the key

Code: Select all  Expand view

# include "fivewin.ch"
# ifdef __HARBOUR__
# ifndef __XHARBOUR__
# include "xhb.ch"
//Correct this line in hbcompat.ch
#xtranslate HSETAACOMPATIBILITY(<x>,<y>) => hb_HKEEPORDER(<x>,<y>)
# endif
# endif
FUNC MAIN
LOCAL aStruct := {{"Name","C",20,0},{"Adres","C",20,0},{"City","C",20,0}}
LOCAL h , i , cKey , txt := ""
DbCreate("Testdbf",aStruct)
USE TESTDBF
IF RECCOUNT() == 0
  APPEND BLANK
  h := ReadDbfRec()
  h["NAME"]  := "Demont Frank"
  h["ADRES"] := "Street"
  h["CITY"]  := "Antwerp"
  WriteDbfRec(h)
ELSE
  h := ReadDbfRec()
END
// Works only in XHARBOUR
# ifdef __XHARBOUR__
// In Xharbour we can use FIELD->City , Fieldget(3) , h[3] , h["CITY"] , all the same
Txt := "Creation order" + CRLF
FOR i := 1 TO FCOUNT()
  Txt += PAD(FieldName(i),8) + h[i] + CRLF
NEXT
# endif
txt += CRLF + CRLF + "Hash order" + CRLF
// Xharbour should give afabetic order , harbour creation order (hb_hKeepOrder(h,.T.) !)
FOR EACH cKey IN hGetKeys(h)
  Txt += PAD(cKey,8) + h[cKey] + CRLF
NEXT
? Txt
CLOS ALL
RETURN nil
********************************
FUNC ReadDbfRec()
LOCAL h := TAssociativeArray()
LOCAL i
FOR i := 1 TO FCOUNT()
  h[FieldName(i)] := Fieldget(i)
NEXT
RETURN h
********************************
FUNC WriteDbfRec(h)
LOCAL i , cKey
FOR EACH cKey IN hGetKeys(h)
  EVAL(Fieldblock(cKey , h[cKey]))
NEXT
RETURN h
***********************************
Func TAssociativeArray()
LOCAL h := Hash()
LOCAL lAssociative := HGetAACompatibility( h )
HSETAACOMPATIBILITY(h,.T.) // translated in hb_hKeepOrder(h,.T.) (harbour)
? lAssociative , HGetAACompatibility( h )
RETURN h
 
test
Franklin Demont
 
Posts: 166
Joined: Wed Aug 29, 2012 8:25 am

Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 27 guests