xHarbour Reference Documentation > Function Reference xHarbour Developers Network  

HaaGetPos()

Retrieves the ordinal position of a key in an associative array.

Syntax

HaaGetPos( <hArray>, <xKey> ) --> nPos

Arguments

<hArray>
A variable referencing the associative array to search a key in.
<xKey>
This is the key to search for in <hArray>.

Return

The function returns the ordinal position of <xKey> in the associative array <hArray>, or 0 if the key is not present.

Description

This function is mostly used to check if a key is present in an associative array. If the return value is greater than zero, the key exists. The return value can then be passed on to function HaaGetValueAt() to retrieve the associated value. This is more efficient than using the key a second time for retrieving the value.

Info

See also:Array(), HaaGetKeyAt(), HaaGetRealPos(), HaaGetValueAt(), HaaSetValueAt(), Hash(), HSetAACompatibility()
Category: Associative arrays , Hash functions , xHarbour extensions
Source:vm\hash.c
LIB:xhb.lib
DLL:xhbdll.dll

Example

// The example creates an associative array and retrieves values
// from it using a function and array notation.

   PROCEDURE Main
      LOCAL hArray := Hash()

      HSetAACompatibility( hArray, .T. )

      hArray[ "One"  ] := 10
      hArray[ "Two"  ] := 20
      hArray[ "Three"] := 30
      hArray[ "Four" ] := 40
      hArray[ "Five" ] := 50

      ? HaaGetPos( hArray, "Four" )   // result: 4
      ? HaaGetPos( hArray, "Five" )   // result: 5
      ? HaaGetPos( hArray, "Six"  )   // result: 0

      ? HaaGetValueAt( hArray, 4 )    // result: 40
      ? HaaGetValueAt( hArray, 5 )    // result: 50

      ? hArray[4]                     // result: 40
      ? hArray[5]                     // result: 50
   RETURN

Copyright © 2006-2007 xHarbour.com Inc. All rights reserved.
http://www.xHarbour.com
Created by docmaker.exe