xHarbour Reference Documentation > Function Reference |
Retrieves the ordinal position of a key in an associative array.
HaaGetPos( <hArray>, <xKey> ) --> nPos
The function returns the ordinal position of <xKey> in the associative array <hArray>, or 0 if the key is not present.
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.
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 |
// 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
http://www.xHarbour.com