Página 1 de 1

Duvida com Hash Table

Enviado: 30 Nov 2017 07:45
por asimoes
Pessoal,

Estou estudando o recurso hash table e queria saber como é que funciona a função hb_HScan no exemplo abaixo:

Código: Selecionar todos

   hTable := { => }
   
   FOR I:=1 TO 5
      hRecord := { => }
      hb_HSet( hRecord, "CODIGO",     StrZero(i, 5) )
      hb_HSet( hRecord, "NOME",       "ALEXANDRE" )
      hb_HSet( hTable, hb_NtoS( I ), hRecord )
   NEXT

   * ??? 
   nPos := hb_HScan( hTable, "00003" ) 


Duvida com Hash Table

Enviado: 30 Nov 2017 08:02
por asimoes
Descobri:

nPos := hb_HScan( hTable, {| nKey, cChave | cChave["CODIGO"] = "00003"} )

Agora pode apagar o elemento

hb_HDelAt( hTable, nPos )

Duvida com Hash Table

Enviado: 30 Nov 2017 08:22
por asimoes
Trocando valor

nPos := hb_HScan( hTable, {| nKey, cChave | cChave["CODIGO"] = "00003"} )
hTable[hb_NtoS(nPos), "CODIGO"] := "teste" //trocando valor na table

Muito bom, agora já dá pra brincar.

Duvida com Hash Table

Enviado: 30 Nov 2017 08:38
por asimoes
Mais informações:

https://github.com/Petewg/harbour-core/wiki/hb_H

Agora só resta saber como usar hb_HSort() fiz hb_HSort htable) mas vi o sort funcionar

Duvida com Hash Table

Enviado: 30 Nov 2017 10:05
por asimoes
Outra forma de pegar o valor de uma chave

Código: Selecionar todos

   cReg := "3"
   cCodigo := hb_HGetDef(hTable, cReg, "teste")
   ? cCodigo["CODIGO"]

Duvida com Hash Table

Enviado: 30 Nov 2017 10:21
por asimoes
Outro exemplo:

Código: Selecionar todos

nPos := hb_HScan( hTable, {| nKey, cChave | cChave["CODIGO"] = "00003"} )
aResult := hb_HPairAt( hTable, nPos ) 
hwg_MsgInfo(aResult[2]["CODIGO"])

Duvida com Hash Table

Enviado: 30 Nov 2017 10:50
por asimoes
Similar a função AEval()

Código: Selecionar todos

hb_HEval( hTable, {| k,cKey |  IF( cKey["CODIGO"] == "00030", cKey["CODIGO"] := "000100", cKey["CODIGO"] := cKey["CODIGO"]), QOut( cKey["CODIGO"]+ ' ' + cKey["NOME"] ) } ) 

Duvida com Hash Table

Enviado: 30 Nov 2017 11:16
por asimoes
Corrigindo o meu post

Agora só resta saber como usar hb_HSort() fiz hb_HSort( htable) mas não vi o sort funcionar