On Tue, 25 May 2010, Szak�ts Viktor wrote:

Hi,

> That sounds great, may I ask for a simple example which 
> show the usefulness of this feature over regular hashes?


This code illustrates the difference between regular and
associative hash arrays:

   proc main()
      local h
      h := { => }
      ? "regular hash array"
      tst( h )
      h := { => }
      ? "associative hash array"
      hb_hKeepOrder( h, .T. )
      tst( h )
   return
   proc tst( h )
      local v, i
      h[ 'zxc' ] := 1.000
      h[ 'qwe' ] := 2.000
      h[ 'abc' ] := 3.000
      h[ 'dfg' ] := 4.000
      h[ 'rbc' ] := 5.000
      ? "for each:"
      for each v in h
         ? v:__enumIndex(), v:__enumKey(), v
      next
      ? "access by index:"
      for i := 1 to len( h )
         ? i, hb_hValueAt( h, i )
      next
      ?
   return

As you can see executing it the order of items in associative array
is strictly defined and it's the same as the order in which new item
were added. It means that you have two strictly defined indexes. One
which can be used with [] operator and any hash item keys and the
second numeric only index. It's usable in many different places.
This is the part of ChangeLog message I created for Harbour OLE
servers. It's very nice example of practical usage.

   <hAction> is optional parameter with hash array containing messages
   and instance variables used by OLE server. The keys in hash array
   are strings with message names and values are actions. Codeblock
   and symbol items means that given message is a method call and
   any other value means that it's variable.
   By default the same hash array is shared between all objects
   created by registered server. It's important when hash array
   contains values which are neither codeblock nor symbol items
   so they are not used as method but rather as instance variables
   because such instance variables are shared between OLE objects.
   Setting 4-th parameter <lHashClone> to .T. causes that each
   objects receives it's own copy of <hAction> item so instance
   variables inside hash array are also local to OLE object.
   Alternatively programmer can use <bAction> or <sAction> to create
   seprate copy of hash array for each object, i.e.:
      bAction := {|| hb_hClone( hValue ) }
   When hash array contains symbol item (@funcName()) then when it's
   executed by OLE object message it's possible to access the hash
   array bound with given OLE object using QSelf() function. It maybe
   useful if hash array contains instance variables and programmer
   wants to access them.
   Please remember that using hash array which was initialized to keep
   original assign order by HB_HKEEPORDER( <hAction>, .T. ) before
   adding its items you can define strict message numbers (DISPIDs), i.e.:
      hAction := {=>}
      HB_HKEEPORDER( hAction, .T. )
      hAction[ "OPEN" ]  := @myole_open()     // DISPID=1
      hAction[ "CLOSE" ] := @myole_close()    // DISPID=2
      hAction[ "SAVE" ]  := @myole_save()     // DISPID=3
      hAction[ "LOAD" ]  := @myole_load()     // DISPID=4
      hAction[ "PRINT" ] := @myole_print()    // DISPID=5
   (see example in olesrv2.prg)

best regards,
Przemek
_______________________________________________
Harbour mailing list (attachment size limit: 40KB)
Harbour@harbour-project.org
http://lists.harbour-project.org/mailman/listinfo/harbour

Reply via email to