Re: hash with (x)harbour - knowledge base
Posted: Fri Sep 24, 2010 8:02 am
A Good example from using hashes is when tabels or dbf's must be totalisized.
See code
See code
- Code: Select all Expand view
//# define NOHASH
FUNCTION MAIN
LOCAL aTabel := {{"CL1",10},{"CL1",20},{"CL1",30},{"CL1",40},{"CL2",10},{"CL2",20}}
LOCAL el , i , ClNr
# ifdef NOHASH
LOCAL aSum[0]
FOR EACH el IN aTabel
ClNr := el[1]
i := ASCAN(aSum , {|x|x[1]==ClNr})
IF i == 0
AADD(aSum,{ClNr,0})
i := LEN(aSum)
END
aSum[i,2] += el[2]
NEXT
FOR EACH el IN aSum
? el[1] , el[2]
NEXT
# else
LOCAL hSum := Hash()
FOR EACH el IN aTabel
ClNr := el[1]
IF ! (ClNr IN hSum)
hSum[ClNr] := 0
END
hSum[ClNr] += el[2]
NEXT
FOR EACH el In hGetKeys(hSum)
? el , hSum[el]
NEXT
# endif
WAIT
RETURN nil