Página 1 de 1

Exemplo Entendendo o Uso de Hash

Enviado: 14 Set 2019 17:21
por asimoes
Achei interessante o exemplo.

Link original: http://www.blacktdn.com.br/2012/01/blac ... z5zVdIA2Ob

Código: Selecionar todos

FUNCTION HASH()
LOCAL hPessoa  := Hb_Hash()
LOCAL hPessoas := Hb_Hash()
LOCAL nPessoa  := 0 
LOCAL nPessoas := 1000


   SET CENTURY ON

   SET DATE TO BRITISH 

   SET DATE FORMAT "mm/dd/yyyy"

   hPessoa["PESSOA"] := Hb_Hash()

   hb_HCaseMatch( hPessoa["PESSOA"] , .F. )

   hPessoa["PESSOA"]["NOME"]        := "BlackTDN"
   hPessoa["PESSOA"]["NASCIMENTO"]  := Ctod("01/01/2012")
   hPessoa["PESSOA"]["SEXO"]        := "M"
   hPessoa["PESSOA"]["PAIS"]        := "Brasil"
   hPessoa["PESSOA"]["ENDERECO"]    := "http://blacktdn.com.br"
   hPessoa["PESSOA"]["CEP"]         := "00000-000"

   ? 'hPessoa["PESSOA"]["NOME"] :' + LTrim(str(Hb_hPos( hPessoa["PESSOA"] , "NOME" ) ))

   ? 'hPessoa["PESSOA"]["NOME"] :' + Hb_hGet( hPessoa["PESSOA"] , "NOME" )

   ? Hb_Eol()
 
   ? 'hPessoa["PESSOA"]["NASCIMENTO"] :' + LTrim(str(Hb_hPos( hPessoa["PESSOA"] , "NASCIMENTO" ) ))

   ? 'hPessoa["PESSOA"]["NASCIMENTO"] :' + dtoc( Hb_hGet( hPessoa["PESSOA"] , "NASCIMENTO" ) )

   ? Hb_Eol()

   ? 'hPessoa["PESSOA"]["SEXO"] :' + LTrim(str(Hb_hPos( hPessoa["PESSOA"] , "SEXO" )))

   ? 'hPessoa["PESSOA"]["SEXO"] :' + Hb_hGet( hPessoa["PESSOA"] , "SEXO" )

   ? Hb_Eol()

   ? 'hPessoa["PESSOA"]["PAIS"] :' + LTrim(str(Hb_hPos( hPessoa["PESSOA"] , "PAIS" )))

   ? 'hPessoa["PESSOA"]["PAIS"] :' + Hb_hGet( hPessoa["PESSOA"] , "PAIS" )

   ? Hb_Eol()
 

   ? 'hPessoa["PESSOA"]["ENDERECO"] :' + LTrim(str(Hb_hPos( hPessoa["PESSOA"] , "ENDERECO" ) ))

   ? 'hPessoa["PESSOA"]["ENDERECO"] :' + Hb_hGet( hPessoa["PESSOA"] , "ENDERECO" )

   ? Hb_Eol()
   
   ? 'hPessoa["PESSOA"]["CEP"] :' + LTrim(str( Hb_hPos( hPessoa["PESSOA"] , "CEP" ) ))

   ? 'hPessoa["PESSOA"]["CEP"] :' + Hb_hGet( hPessoa["PESSOA"] , "CEP" )

   ? Hb_Eol()

   For nPessoa := 1 To 1 //nPessoas

      hPessoas[nPessoa]                         := Hb_hClone(hPessoa)

      hPessoas[nPessoa]["PESSOA"]["NOME"]       += ' ' + StrZero(nPessoa,4)

      IF ( ( nPessoa % 2 ) == 0 )

         hPessoas[nPessoa]["PESSOA"]["NASCIMENTO"] := YearSum( hPessoas[nPessoa]["PESSOA"]["NASCIMENTO"] , nPessoa )

      Else

         hPessoas[nPessoa]["PESSOA"]["NASCIMENTO"] := YearSub( hPessoas[nPessoa]["PESSOA"]["NASCIMENTO"] , nPessoa )

      EndIF

   Next nPessoa

   

   FOR EACH hPessoa in hPessoas

      ? 'hPessoa["PESSOA"]["NOME"] :' + LTrim(str(Hb_hPos( hPessoa["PESSOA"] , "NOME" ) ))

      ? 'hPessoa["PESSOA"]["NOME"] :' + Hb_hGet( hPessoa["PESSOA"] , "NOME" )

      ? Hb_Eol()

      ? 'hPessoa["PESSOA"]["NASCIMENTO"] :' + LTrim(str(Hb_hPos( hPessoa["PESSOA"] , "NASCIMENTO" ) ))

      ? 'hPessoa["PESSOA"]["NASCIMENTO"] :' + dtoc( Hb_hGet( hPessoa["PESSOA"] , "NASCIMENTO" ) )

      ? Hb_Eol()

      ? 'hPessoa["PESSOA"]["SEXO"] :' + LTrim(str(Hb_hPos( hPessoa["PESSOA"] , "SEXO" )))

      ? 'hPessoa["PESSOA"]["SEXO"] :' + Hb_hGet( hPessoa["PESSOA"] , "SEXO" )

      ? Hb_Eol()

      ? 'hPessoa["PESSOA"]["PAIS"] :' + LTrim(str(Hb_hPos( hPessoa["PESSOA"] , "PAIS" )))

      ? 'hPessoa["PESSOA"]["PAIS"] :' + Hb_hGet( hPessoa["PESSOA"] , "PAIS" )

      ? Hb_Eol()

      ? 'hPessoa["PESSOA"]["ENDERECO"] :' + LTrim(str(Hb_hPos( hPessoa["PESSOA"] , "ENDERECO" ) ))

      ? 'hPessoa["PESSOA"]["ENDERECO"] :' + Hb_hGet( hPessoa["PESSOA"] , "ENDERECO" )

      ? Hb_Eol()

      ? 'hPessoa["PESSOA"]["CEP"] :' + LTrim(str( Hb_hPos( hPessoa["PESSOA"] , "CEP" ) ))

      ? 'hPessoa["PESSOA"]["CEP"] :' + Hb_hGet( hPessoa["PESSOA"] , "CEP" )

      ? Hb_Eol()
 

   NEXT EACH \\hPessoa

   inkey(0)

Return( .T. )

 

Function Day2Str( uData )

   Local cType := ValType( uData )

IF ( cType == "D" )

   Return( StrZero( Day( uData ) , 2 ) )

ElseIF ( cType == "N" )

   Return( StrZero( uData , 2 ) )

ElseIF ( cType == "C" )

   Return( StrZero( Val( uData ) , 2 ) )

EndIF

 

Function Month2Str( uData )

   Local cType := ValType( uData )

IF ( cType == "D" )

   Return( StrZero( Month( uData ) , 2 ) )

ElseIF ( cType == "N" )

   Return( StrZero( uData , 2 ) )

ElseIF ( cType == "C" )

   Return( StrZero( Val( uData ) , 2 ) )

EndIF

 

Function Year2Str( uData )

   Local cType := ValType( uData )

IF ( cType == "D" )

   Return( StrZero( Year( uData ) , 4 ) )

ElseIF ( cType == "N" )

   Return( StrZero( uData , 4 ) )

ElseIF ( cType == "C" )

   Return( StrZero( Val( uData ) , 4 ) )

EndIF

 

Function Last_Day( dDate )

 

   Local nMonth

   Local nYear

 

   IF ( ValType( dDate ) == "C" )

      dDate := CToD( dDate )   

   EndIF

 

   nMonth := ( Month( dDate ) + 1 )

   nYear  := Year( dDate )

   IF ( nMonth > 12 )

      nMonth := 1

      ++nYear

   EndIF

 

   dDate := CToD( "01/" + Month2Str( nMonth ) + "/" + Year2Str( nYear ) )

   dDate -= 1

 

Return( Day( dDate ) )

 

Function YearSum( dDate , nYear )

 

   Local nMonthAux := Month( dDate )

   Local nDayAux   := Day( dDate )

   Local nYearAux  := Year( dDate )

 

   nYearAux += nYear

   dDate := Ctod( Day2Str( nDayAux ) + "/" + Month2Str( nMonthAux ) + "/" + Year2Str( nYearAux ) )

   IF Empty( dDate )

      dDate   := Ctod( Day2Str( 1 ) + "/" + Month2Str( nMonthAux ) + "/" + Year2Str( nYearAux ) )

      nDayAux := Last_Day( dDate )

      dDate   := Ctod( Day2Str( nDayAux ) + "/" + Month2Str( nMonthAux ) + "/" + Year2Str( nYearAux ) )

   EndIF

 

Return( dDate )

 

Function YearSub( dDate , nYear )

 

   Local nMonthAux := Month( dDate )

   Local nDayAux   := Day( dDate )

   Local nYearAux  := Year( dDate )

 

   nYearAux -= nYear

   dDate := Ctod( Day2Str( nDayAux ) + "/" + Month2Str( nMonthAux ) + "/" + Year2Str( nYearAux ) )

   IF Empty( dDate )

      dDate   := Ctod( Day2Str( 1 ) + "/" + Month2Str( nMonthAux ) + "/" + Year2Str( nYearAux ) )

      nDayAux := Last_Day( dDate )

      dDate   := Ctod( Day2Str( nDayAux ) + "/" + Month2Str( nMonthAux ) + "/" + Year2Str( nYearAux ) )

   EndIF


Return( dDate )

Exemplo Entendendo o Uso de Hash

Enviado: 14 Set 2019 22:35
por JoséQuintas
hazael escreveu:Talvez o HASH possa ser usado para carregar um arquivo XML da NFe, por exemplo, assim ficaria mais fácil.
Por enquanto no Danfe da sefazclass ainda usa por hash.
Ainda não eliminei isso.

Sinceramente, ainda não vi nada onde isso poderia trazer vantagens.
Não sei no futuro.