Hash Details – 2


( GET / SET Functions )

HB_HGET() : Returns a hash value

Syntax :   HB_HGET( <hsTable>, <Key> ) -> <Value>

HB_HPOS() : Locates the index of a key within a hash table

Syntax :      HB_HPOS( <hsTable>, <Key> ) -> nPosition

HB_HVALUEAT() : Gets/sets a hash value at a given position

Syntax :      HB_HVALUEAT( <hsTable>, <nPosition>, [<NewValue>] ) -> <Value>

HB_HVALUES() : Returns an array of the values of a hash table

Syntax :      HB_HVALUES( <hsTable> ) -> <aValues>

HB_HKEYAT() : Gets a hash table key at a given position

Syntax :      HB_HKEYAT( <hsTable>, <nPosition> ) -> <Key>

HB_HKEYS() : Returns an array of the keys of a hash table

Syntax :      HB_HKEYS( <hsTable> ) -> <aKeys>

HB_HPAIRAT() : Returns a two-dimensional array of a hash table entry key/value pair

Syntax :      HB_HPAIRAT( <hsTable>, <nPosition> ) -> <aKeyValue>

HB_HSCAN() : Scans a hash table for a value

Syntax :  HB_HSCAN( <hsTable>, <Value>, [<nStart>], [<nCount>, [<lExact>] ) -> nPosition

HB_HSET() : Sets a hash value

Syntax :      HB_HSET( <hsTable>, <Key>, <Value> ) -> <hsTable>

*-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.

/*

Hash Details - 2

 ( GET / SET Funtions )

*/
#define xTrim( x ) IF( HB_ISNUMERIC( x ), LTRIM( STR( x ) ), ALLTRIM( x ) )
PROCEDURE Main()
   SetMode( 60, 120 )

   SET COLO TO "W/B"

   cLMarj := SPACE( 3 )

   CLS

   hFruits := { "apple" => "green",;
                "chery" => "pink",; 
                "apricot" => "yellow",;
                "orange" => "orange" }

   ListHash( hFruits, "Fruits" )

   * HB_HGET( <hsTable>, <Key> ) -> <Value>

   ? "Value found by literal of 'apple' : ",; 
      hFruits[ "apple" ] // green
   ? "Value found by function of 'apple' : ",; 
      HB_HGET( hFruits, "apple" ) // green
   ?

   * HB_HSET( <hsTable>, <Key>, <Value> ) -> <hsTable>
   hFruits[ "apple" ] := "red"
   ? "New (assigned literally) Value of 'apple' : ",; 
     hFruits[ "apple" ] // red

   HB_HSET( hFruits, "apple", "yellow" )
   ? "Newer (assigned by function) Value of 'apple' : ",; 
     hFruits[ "apple" ] // yellow
   ? 

   * HB_HPOS( <hsTable>, <Key> ) -> nPosition
   ? "Position of 'apple' : ", ;
     xTrim( HB_HPOS( hFruits, "apple" ) ) // 1
   * HB_HVALUEAT( <hsTable>, <nPosition>, [<NewValue>] ) -> <Value>
   ? "Position of 'melon' : ", ;
      xTrim( HB_HPOS( hFruits, "melon" ) ) // 0
   nPos := HB_HPOS( hFruits, "apricot" )
   ? "Position of 'apricot' : ",; 
     xTrim( HB_HPOS( hFruits, "apricot" ) ) // 4
   ?
   ? "Value of ", xTrim( nPos ) + ".th position :", ;
   HB_HVALUEAT( hFruits, nPos ) // yellow
   ?
   HB_HVALUEAT( hFruits, nPos, "fushia" ) 

   ? "Changed value in this position :",; 
   HB_HVALUEAT( hFruits, nPos ) // fushia
   ? 

   * HB_HKEYAT( <hsTable>, <nPosition> ) -> <Key>
   ? "Key in this position :" ,; 
     HB_HKEYAT( hFruits, nPos ) // apricot
   ?

   * HB_HKEYS( <hsTable> ) -> <aKeys>

   aKeys := HB_HKEYS( hFruits ) 

   ? "All keys in 'hFruits' : "
   AEVAL( aKeys, { | cKey | QQOUT( cKey, '' ) } ) 
                    // apple chery orange apricot
   ?

   * HB_HVALUES( <hsTable> ) -> <aValues>
   aValues := HB_HVALUES( hFruits ) 

   ? "All values in 'hFruits' : "
   AEVAL( aValues, { | cValue | QQOUT( cValue, '' ) } ) 
                     // green pink orange yellow
   ?

   * HB_HPAIRAT( <hsTable>, <nPosition> ) -> <aKeyValue>

   ? "All pairs in 'hFruits' : "

   Note : Compare this loop with below ListHash()

   FOR nPos := 1 TO LEN( hFruits )
      aKey_Value := HB_HPAIRAT( hFruits, nPos ) 
      ? xTrim( nPos ), aKey_Value[ 1 ], "=>", aKey_Value[ 2 ]
   NEXT
   ?

   * HB_HSCAN( <hsTable>, <Value>, [<nStart>], [<nCount>, [<lExact>] ) 
   *                                                     -> nPosition
   ? "Position found by scanning value 'pink' : ",;
   xTrim( HB_HSCAN( hFruits, "pink" ) ) // 2
   ?
   @ MAXROW(), 0
   WAIT "EOF HashDetails-2.prg"

RETURN // HashDetails-2.Main()
*-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.
PROCEDURE ListHash( hHash, cComment )

 LOCAL x1Pair

   cComment := IF( HB_ISNIL( cComment ), '', cComment )

   ? cComment, '' 
   * ?? "-- Type :", VALTYPE( hHash ),'' 
   * ?? "size:", NTrim ( LEN( hHash ) ) 
   ?
   FOR EACH x1Pair IN hHash
      nIndex := x1Pair:__ENUMINDEX()
      x1Key := x1Pair:__ENUMKEY()
      x1Value := x1Pair:__ENUMVALUE()
      ? cLMarj, xTrim( nIndex ) 
      * ?? '', VALTYPE( x1Pair )
      ?? '', xTrim( x1Key ), "=>"
      * ?? '', VALTYPE( x1Key ) 
      * ?? VALTYPE( x1Value ) 
      IF HB_ISARRAY( x1Value ) 
        AEVAL( x1Value, { | x1 | QQOUT( '', x1 ) } )
      ELSE 
        ?? '', xTrim( x1Value )
      ENDIF 
   NEXT

   ? REPL( "~", 32 ) 

RETURN // ListHash()
*-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.-._.
hashdets-2
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.