hua wrote:How to create a 2-dimensional associative array? For example let's consider something similar to a spreadsheet where the columns are A to E, the rows are 1 to 6 [equivalent to array(5,6) matrix]
The few questions that I have are:
i. How to initialize it? [I mean its array(5,6) equivalent]
ii. Would this be the correct way to create
and initialize the hash table and its values?
- Code: Select all Expand view RUN
oTable := hash()
oTable["A"]["1"] := 0
oTable["B"]["1"] := 0
oTable["C"]["1"] := 0
oTable["D"]["1"] := 0
oTable["E"]["1"] := 0
oTable["A"]["2"] := 0
.
.
oTable["A"]["6"] := 0
oTable["B"]["6"] := 0
oTable["C"]["6"] := 0
oTable["D"]["6"] := 0
oTable["E"]["6"] := 0
iii. Does an associative array has something similar to asort()?
That's all for now. TIA
It Could be done as :
oTabel := Hash()
HsetAAcompability(oTable , .T.)
FOR i := 1 TO 6
//oTabel["A"] := Hash()
oTabel[CHR(64+i)] := Hash()
HsetAAcompability(oTabel[CHR(64+i)],.T.)
FOR j := 1 TO 6
c := LTRIM(STR(j))
oTabel[CHR(64+i),c] := 0
NEXT
NEXT
Elements can be retrieved as oTabel["A","1"] or as oTabel[1,1] or both mixed
A better aproach would be :
oTabel := Hash()
FOR i := 1 TO 6
oTabel[CHR(64+i)] := Array(6)
aFill(oTabel[CHR(64+i)],0)
//FOR j := 1 TO 6
// oTabel[CHR(64+i),j] := 0
//NEXT
NEXT
Elements can only be retrieved as oTabel["A",1]
Note that a hash() has only the properties from a associative array when is used
HsetAAcompability(<hHash>,<lToggle>) . In that case the creation order is respected and the elements can be retrieved with their numeric ordinal position (i.e. oTabel[1,"1"])
A hash is ordered on the key (in this example it makes no difference) , it can only be retrieved with the key (i.e. oTabel["A",1])
Frank