Hash - Orden

Post Reply
User avatar
MarioG
Posts: 1380
Joined: Fri Oct 14, 2005 1:28 pm
Location: Resistencia - Chaco - AR
Been thanked: 2 times

Hash - Orden

Post by MarioG »

Gene;
Asi como puedo ordenar un array, bidimensional, puedo ordenar un Hash por su Valor?

gracias
Resistencia - "Ciudad de las Esculturas"
Chaco - Argentina
xmanuel
Posts: 768
Joined: Sun Jun 15, 2008 7:47 pm
Location: Sevilla
Been thanked: 5 times
Contact:

Re: Hash - Orden

Post by xmanuel »

Hola Mario :-)

Usa esta funcion:

function miOrdenHash( hashArray )
return( ASort( hb_HValues( hashArray ) ) )

Espero que te funcione.

8)
______________________________________________________________________________
Sevilla - Andalucía
User avatar
MarioG
Posts: 1380
Joined: Fri Oct 14, 2005 1:28 pm
Location: Resistencia - Chaco - AR
Been thanked: 2 times

Re: Hash - Orden

Post by MarioG »

Hola Manu!
gracias por responder
Hice la prueba así:

Code: Select all | Expand

PROCEDURE Main()
loca hashVar:= Hash( "a", "Zaragosa", ;
                     "b", "Klein"   , ;
                     "c", "Romero"  , ;
                     "d", "Gonzalez", ;
                     "e", "Baranda" )

   HEval( hashVar, {|key,val| wqout({key,val}) })
   ? "Ordena?"
   ASort( hb_HValues( hashVar ) )
   HEval( hashVar, {|key,val| wqout({key,val}) })          // No ordena
   ? "Ordena?"
   hashVar := ASort( hb_HValues( hashVar ) )
   HEval( hashVar, {|key,val| wqout({key,val}) })          // Da error de argumento en el hash
 

que estoy haciendo mal?

P.D: larga vida a TDBF! :wink:
Resistencia - "Ciudad de las Esculturas"
Chaco - Argentina
xmanuel
Posts: 768
Joined: Sun Jun 15, 2008 7:47 pm
Location: Sevilla
Been thanked: 5 times
Contact:

Re: Hash - Orden

Post by xmanuel »

La función que te envié devuelve un vector ordenado solo con los valores ;-)
Prueba esto así:

Code: Select all | Expand


local hashVar:= Hash( "a", "Zaragosa", ;
                     "b", "Klein"   , ;
                     "c", "Romero"  , ;
                     "d", "Gonzalez", ;
                     "e", "Baranda" )

local aValues := ASort( hb_HValues( hashVar ) )

   aEval( aValues, {|val| wqout( val ) })          
 


La verdad es que no sé para qué quieres un hash ordenado? o sólo quieres los valores ordenados?

A ver si te puedo ayudar?
:mrgreen:
______________________________________________________________________________
Sevilla - Andalucía
User avatar
MarioG
Posts: 1380
Joined: Fri Oct 14, 2005 1:28 pm
Location: Resistencia - Chaco - AR
Been thanked: 2 times

Re: Hash - Orden

Post by MarioG »

Manu;
claro!, disculpá la torpeza, no se ordena el hash, sino se crea un array ordenado
La idea del orden es por lo siguiente.
Resulta que tengo un xBrowse con nombres de personas, donde se las va seleccionando, esta ordenado por un numero de ID, de manera que la búsqueda es mas agil.
Cada registro marcado, mediante la tecla space, hace lo siguiente:

Code: Select all | Expand

     :bKeyDown    := {|nK| if( nK==VK_SPACE,  ( oQry:LoadQuery(), ;
                                                 if( HHasKey( hSelec, oQry:id_player ), ( HDel( hSelec, oQry:id_player ) ), ;
                                                                                        ( HSet( hSelec, oQry:id_player, oQry:ApeyNom ), oBrw:GoDown() ) ), ;
                                                 if( Len(hSelec) >= 1, aO[_BtOk]:Enable(), aO[_BtOk]:Disable() ) ), ) }
 
Demanera que cada registro marcado cambia de color.
Esta técnica de selección la uso porque la aplicación se usará en RED.
Ahora bien, cuando termina la selección, se la vuelca a otro xBrowse... si miras en la fecha de este post, vas a encontrar otro, donde hice otra consulta: "Mostrar un hash en xBrowse"; que no tuvo respuesta.
O sea finalmente, la idea es mostrar este segundo browse ordenado.
Pero como no sé como mostrar un Hash en un xBrowse, tu solución me sirve, para que el Hash me devuelva un array ordenado que es lo que puedo mostrar en ese segundo xBrowse
mareado... leeme despacio jejejeje
Resistencia - "Ciudad de las Esculturas"
Chaco - Argentina
xmanuel
Posts: 768
Joined: Sun Jun 15, 2008 7:47 pm
Location: Sevilla
Been thanked: 5 times
Contact:

Re: Hash - Orden

Post by xmanuel »

Si aún así por algún motivo quieres tener la key también pueder crear un array bidimensional que tambien se puede mostrar sin problema en el browse:

Code: Select all | Expand


aKeysValues := {
                         { k1, v1 },;
                         { k2, v2 },;
                         ...
                       }
 


Recuerda que además de la función hb_HValues( hashVar ) está hb_HKeys( hashVar ), construir un aKeysValues es fácil y que ASort admite un codeblock para ordenar:
ASORT(aKeysValues ,,, { |x, y| x[2] < y[2] }
______________________________________________________________________________
Sevilla - Andalucía
User avatar
MarioG
Posts: 1380
Joined: Fri Oct 14, 2005 1:28 pm
Location: Resistencia - Chaco - AR
Been thanked: 2 times

Re: Hash - Orden

Post by MarioG »

Entendido;
nuevamente gracias!
Resistencia - "Ciudad de las Esculturas"
Chaco - Argentina
User avatar
leandro
Posts: 1770
Joined: Wed Oct 26, 2005 2:49 pm
Location: Colombia
Has thanked: 49 times
Been thanked: 12 times
Contact:

Re: Hash - Orden

Post by leandro »

Buenos días para todos,

Quisiera saber si ya existe alguna función o si directo desde xbrowse se puede cambiar el orden del hash. Ya que estoy tratando de compilar este ejemplo pero no me funciona.

Code: Select all | Expand


#include "FiveWin.Ch"
#include "ord.ch"
#include "xbrowse.ch"

PROCEDURE Main()
loca hashVar:= Hash( "a", "Zaragosa", ;
                     "b", "Klein"   , ;
                     "c", "Romero"  , ;
                     "d", "Gonzalez", ;
                     "e", "Baranda" )

   HEval( hashVar, {|key,val| wqout({key,val}) })
   ? "Ordena?"
   ASort( hb_HValues( hashVar ) )
   HEval( hashVar, {|key,val| wqout({key,val}) })          // No ordena
   ? "Ordena?"
   hashVar := ASort( hb_HValues( hashVar ) )
   HEval( hashVar, {|key,val| wqout({key,val}) })          // Da error de argumento en el hash
 

El error

Code: Select all | Expand


┌────────────────────────────────────────────────────────────────────────────┐
?FiveWin for xHarbour 19.09 - Sep. 2019          xHarbour development power │▄
?(c) FiveTech 1993-2019 for Microsoft Windows 9X/NT/200X/ME/XP/Vista/7/8/10 │█
└────────────────────────────────────────────────────────────────────────────┘?
  ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀?
Compiling...
xHarbour 1.2.3 Intl. (SimpLex) (Build 20190613)
Copyright 1999-2018, http://www.xharbour.org http://www.harbour-project.org/
Compiling 'leandro3.prg' and generating preprocessed output to 'leandro3.ppo'...
Generating C source output to 'leandro3.c'...
Done.
Lines 20, Functions/Procedures 1, pCodes 69
Embarcadero C++ 7.30 for Win32 Copyright (c) 1993-2017 Embarcadero Technologies, Inc.
leandro3.c:
Turbo Incremental Link 6.80 Copyright (c) 1997-2017 Embarcadero Technologies, Inc.
Error: Unresolved external '_HB_FUN_HB_HVALUES' referenced from C:\FWH1909\SAMPLES\LEANDRO3.OBJ
Error: Unable to perform link
* Linking errors *
 
Saludos
LEANDRO AREVALO
Bogotá (Colombia)
https://hymlyma.com
https://hymplus.com/
leandroalfonso111@gmail.com
leandroalfonso111@hotmail.com

[ Turbo Incremental Link64 6.98 Embarcadero 7.70 ] [ FiveWin 24.09 ] [ xHarbour 64 bits) ]
User avatar
MarioG
Posts: 1380
Joined: Fri Oct 14, 2005 1:28 pm
Location: Resistencia - Chaco - AR
Been thanked: 2 times

Re: Hash - Orden

Post by MarioG »

Buenos dias Leandro
Interesante tema. Nunca lo hice

En la Wiki de Harbour veo que existe: HB_HSort()
https://github.com/zgamero/sandbox/wiki/2.7-Hashes

Salvo q estes usando xHarbour, deberias ver si tiene la función
Resistencia - "Ciudad de las Esculturas"
Chaco - Argentina
User avatar
leandro
Posts: 1770
Joined: Wed Oct 26, 2005 2:49 pm
Location: Colombia
Has thanked: 49 times
Been thanked: 12 times
Contact:

Re: Hash - Orden

Post by leandro »

Recuerda que además de la función hb_HValues( hashVar ) está hb_HKeys( hashVar ), construir un aKeysValues es fácil y que ASort admite un codeblock para ordenar:
ASORT(aKeysValues ,,, { |x, y| x[2] < y[2] }


Lo pude solucionar utilizando la función aSort()

Code: Select all | Expand


ASORT( aArti,,, { |x,y| x["i_articu"] < y["i_articu"] } )
 
Saludos
LEANDRO AREVALO
Bogotá (Colombia)
https://hymlyma.com
https://hymplus.com/
leandroalfonso111@gmail.com
leandroalfonso111@hotmail.com

[ Turbo Incremental Link64 6.98 Embarcadero 7.70 ] [ FiveWin 24.09 ] [ xHarbour 64 bits) ]
User avatar
nageswaragunupudi
Posts: 10729
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Been thanked: 10 times
Contact:

Re: Hash - Orden

Post by nageswaragunupudi »

Code: Select all | Expand

function SortHash()

   local hHash := Hash( "a", "Zaragosa", ;
                     "b", "Klein"   , ;
                     "c", "Romero"  , ;
                     "d", "Gonzalez", ;
                     "e", "Baranda" )
   local aPairs, hSort

   XBROWSER hHash TITLE "ORIGINAL HASH"

   aPairs   := {}
   HEval( hHash, { | k, v, i | AAdd( aPairs, { k, v } ) } )
   ASort( aPairs, nil, nil, { |x,y| x[ 2 ] < y[ 2 ] } )
   hSort    := {=>}
   AEval( aPairs, { |a| HB_HSet( hSort, a[ 1 ], a[ 2 ] ) } )

   XBROWSER hSort TITLE "SORTED HASH"

return nil
 


Image
Image
Regards

G. N. Rao.
Hyderabad, India
User avatar
leandro
Posts: 1770
Joined: Wed Oct 26, 2005 2:49 pm
Location: Colombia
Has thanked: 49 times
Been thanked: 12 times
Contact:

Re: Hash - Orden

Post by leandro »

Mr Nages, buenos días

Intente compilar con las funciones de xharbour, pero parece no funcionar.

Code: Select all | Expand



#include "Fivewin.ch"

function main()

   local hHash := Hash( "a", "Zaragosa", ;
                     "b", "Klein"   , ;
                     "c", "Romero"  , ;
                     "d", "Gonzalez", ;
                     "e", "Baranda" )
   local aPairs, hSort

   XBROWSER hHash TITLE "ORIGINAL HASH"

   aPairs   := {}
   HEval( hHash, { | k, v, i | AAdd( aPairs, { k, v } ) } )
   ASort( aPairs, nil, nil, { |x,y| x[ 2 ] < y[ 2 ] } )
   hSort    := {=>}
   AEval( aPairs, { |a| HSet( hSort, a[ 1 ], a[ 2 ] ) } )

   XBROWSER hSort TITLE "SORTED HASH"

return nil
 


Image
Saludos
LEANDRO AREVALO
Bogotá (Colombia)
https://hymlyma.com
https://hymplus.com/
leandroalfonso111@gmail.com
leandroalfonso111@hotmail.com

[ Turbo Incremental Link64 6.98 Embarcadero 7.70 ] [ FiveWin 24.09 ] [ xHarbour 64 bits) ]
User avatar
nageswaragunupudi
Posts: 10729
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Been thanked: 10 times
Contact:

Re: Hash - Orden

Post by nageswaragunupudi »

xHarbour always stores the key/value pairs in the order of the key.

Please test this small example:

Code: Select all | Expand

  hHash := hash( "Z", "AA", "A", "ZZ" )
   XBROWSER hHash
 
Regards

G. N. Rao.
Hyderabad, India
Post Reply