Arrays - Petewg/harbour-core GitHub Wiki
-
AAdd(
<aArray>, <xValue>
) β xValue
adds an element to the end of<aArray>
and assigns the value<xValue>
to this new element. -
AClone(
<aSource>
) β aDuplicate
duplicates an array. Works with multidimensional arrays. -
ACopy(
<aSource>, <aTarget>, [<nStart>], [<nCount>], [<nTagetPos>]
) β aTarget
copy elements from a source array into a target array. Operates only on one-dimensional arrays. -
ADel(
<aArray>, <nPosition>
) β aArray
deletes the element at position<nPosition>
from the array<aArray>
. All subsequent elements are shifted up by one position so that the last element contains the valueNIL
.
See also hb_ADel() (below). -
hb_ADel(
<aArray> [, <nPos>, <lAutoSize>]
) β aArray
Deletes the value at position<nPos>
of array<aArray>
moving all the following values to the previous position. If<lAutoSize>
is .T., then the last element is deleted and the size of the array is decreased by one, otherwiseNIL
will be stored in the last element of the array, as in 'ADel()'. Default values for optional parameters:<nPos>
= 1,<lAutoSize>
= .F. -
AEval(
<aArray>, <bBlock>, [<nStart>], [<nCount>]
) β aArray
Evaluates the<bBlock>
for each<aArray>
element, starting from<nStart>
until<nCount>
elements have been reached. Default values for<nStart>/<nCount>
optional parameters:<nStart>
= 1,<nCount>
= Len( aArray ) (i.e. all elements). -
AFill(
<aArray>, <xValue>, [<nStart>], [<nCount>]
) β aArray
assigns the value of<xValue>
to the<nCount>
elements, beginning at position<nStart>
. Multi-dimensional arrays cannot be filled using 'AFill()'. Default values for<nStart>/<nCount>
optional parameters:<nStart>
= 1,<nCount>
= Len( aArray ) (i.e. all elements). -
AIns(
<aArray>, <nPosition>
) β aArray
Inserts a new element at<nPosition>
into the array<aArray>
. All subsequent elements are shifted down by one position so that the last element is lost. The number of elements remain unchanged. Seehb_AIns()
which allows auto-sizing of array.
See also hb_AIns() (below). -
hb_AIns(
<aArray> [, <nPos>, <uValue>, <lAutoSize>]
) β aArray
Inserts<uValue>
in the<nPos>
position of the array, moving all the items to the next position. If<lAutoSize>
is .T., a new element will be added, making room for the previous last element, else the size of<aArray>
won't change and the last item of<aArray>
will be lost. Default values for optional parameters:<nPos>
= 1,<uValue>
= NIL,<lAutoSize>
= .F. -
Array(
<nDim1> [, <nDimN,...>]
) β aArray
returns an array dimensioned according to the passed parameters. All elements of the array contain the valueNIL
. -
AScan(
<aArray>, <xSearch>, [<nStart>], [<nCount>]
) β nStoppedAt
search into<aArray>
for<xSearch>
value starting from<nStart>
up to<nCount>]
. (default values are:nStart=1
,nCount=All elements
).
returns the position of element that matches the searched value or0
if no match is found.
See also hb_AScan() (below). -
hb_AScan(
<aArray>, <uValue> [, <nStart>, <nCount>, <lExact> ]
) β nPosition
Scans (left to right) for<uValue>
into<aArray>
and returnsnPosition
of found orzero
if nothing found. If<lExact>
is given, overrides the_SET_EXACT
setting. Case sensitive on string search.
Note: In hb 3.4 (Viktor's fork) there is a hb_AScanI() which is same as hb_AScan() but with case-insensitive string comparison. See also hb_RAScan() (below). -
hb_RAScan(
<aArray>, <uValue> [, <nStart>, <nCount>, <lExact>]
) β nPosition
scans (right to left) for<uValue>
into<aArray>
and returns<nPosition>
of found or<zero>
if nothing found. -
ASize(
<aArray>, <nLength>
) β aArray
dynamically increases or decreases the size of<aArray>
to<nLength>
elements. When the array is enlarged, new elements are added to the end of<aArray>
and are initialized withNIL
. When the number of elements is decreased, elements are removed from the end of<aArray
and their values are lost. returns reference to sized array. -
ASort(
<aArray>, [<nStart>], [<nCount>], [<bSort>]
) β aArray
sorts an array entirely or partially. If the code block<bSort>
is omitted, the function expects<aArray>
to be a one dimensional array containing simple data types.
NOTES: 1) Sorting is case sensitive for character elements. 2) Logical value.F.
is considered smaller than.T.
-
ATail(
<aArray>
) β xValue
returns the value stored in the last element of<aArray>
.
(also known as associative arrays and hash tables)
-
hb_Hash(
[ <Key1>, <Value1> ], [ <KeyN>, <ValueN> ], ...
) β hTable
Creates a hash table and, optionally, populates it with elements (pairs of keys and values).- keys can be of type: string, numeric, date, datetime, pointer
- values can be of type: string, numeric, date/datetime, logical, array, hash table, pointer, block, NIL
usage:
hHash := hb_Hash( xkey1, xValue1, xKey2, xValue2, ..., xKeyN, xValueN )
An alternative method to create/initialize a hash table, might be to use a direct in-line assignment like below:
// create an empty hash table hHash := {=>} // create & initialize a hash hHash := { 1 => "Apples", 2 => "Oranges", "D1" => 0d20180621 }
This method is, apparently, the most recommended, since it helps out code clarity.
-
hb_HAllocate(
<hHash>, <nElements>
)
Pre-allocates space (memory) for<nElements>
hash items. It's useful when you intend to add a set of new items because you will save time spent in grabbing each new element separately; preallocating elements may improve speed execution of program.
Note: this function will not "truncate" or release (delete) items, if the<nElements>
number is less than the actual item count already into the hash table. -
hb_HAutoAdd(
<hHash>, [<lFlag>]
) β lPreviousFlag
Sets the 'auto add' flag for the hash table. -
hb_HBinary(
<hHash>, [<lFlag>]
) β lPreviousFlag
Sets the 'binary' flag for the hash table. -
hb_HCaseMatch(
<hHash>, [<lFlag>]
) β lPreviousFlag
Sets the 'case match' flag for the hash table. -
hb_HClear(
<hHash>
) β hHash
Deletes all the elements in<hHash>
leaving an empty hash table. -
hb_HClone(
<hHash>
) β hsDestination
Creates a copy of a hash table. -
hb_HCopy(
<hsDestination>, <hsSource>, [<nStart>], [<nCount>]
) β hsDestination
Adds entries from the source hash table to the destination hash table. -
hb_HDefault(
<hHash>, <DefaultValue>
) β OldDefaultValye
Sets the default value assinged to new keys in a hash table. Returns the previous default value (if any). -
hb_HDel(
<hHash>, <xKey>
) β hHash
Deletes the<xKey>
key (as well as, its associated value) from the<hHash>
hash table. If<xKey>
doesn't exist then nothing happens, that is,<hHash>
remains (and returned) unaltered.Important Note: Unlike a conventional array, after a successful deletion of an item (key/value pair), the hash table automatically shrinks and its total length decrements by one.
-
hb_HDelAt(
<hHash>, <nPosition>
) β hHash
Removes (deletes) the item ('key,value' pair) found in<nPosition>
(ordinal position) of the<hHash>
table.
Valid values for<nPosition>
are in range1 .. Len(hHash)
, which means that if<nPosition>
is zero or greater than the length of hash, an RTE (Bound error: array assign) occurs. -
hb_HEval(
<hHash>, <bBlock>, [<nStart>], [<nCount>]
) β hHash
evaluates a code block across the contents of a hash table. It is similar to AEval() function but applied on hashes instead of plain arrays. -
hb_HFill(
<hHash>, <Value>
) β hHash
sets the values of all the keys of<hHash>
table to<xValue>
. -
hb_HGet(
<hHash>, <Key>
) β xValue
retrieves the value associated to<Key>
key.
Hint: use hb_HGetDef() instead of this function, to avoid RTE when the<Key>
key doesnβt exist/found in<hHash>
. -
hb_HGetDef(
<hHash>, <Key>, [<DefaultValue>]
) β xValue|NIL
returns either the value associated to the given<Key>
key into<hHash>
hash table, or the default value<DefaultValue>
if the<Key>
doesnβt exist.
It returnsNIL
when both the<Key>
doesn't exist and no<DefaultValue>
has specified. -
hb_HGetRef(
<hHash>, <xKey>, [ @<xValue> ]
) β .T.|.F.
returns.T.
if<xKey>
exists.
Optionally stores its associated key-value to@<xValue>
which must be passed by reference.
If the key is not found, the function returns.F.
and the<xValue>
becomesNIL
. -
hb_HHasKey(
<hHash>, <xKey> [, @<nPos> ]
) β lExists
returns .T. if<xKey>
exists in the hash table and optionaly stores its ordinal position to@<nPos>
which must be passed by reference to retain the stored position.
If the key doesnβt exists then<nPos>
will be the position of the smaller nearest key. -
hb_HKeepOrder(
<hHash> [ , <lKeepOrder> ]
) β lPrevKeepOrder
Sets theKEEPORDER
flag in a hash and returns the previous state. This flag controls the order by which the elements are stored in the hash table. If theKEEPORDER
flag is.T.
then the order by which the elements are created or added will be preserved. If it is.F.
, then the elements of the hash table will be sorted (other flags may affect the order such as theBINARY
flag, theCASEMATCH
flag, etc). This flag is enabled (.T.
) by default. -
hb_HKeyAt(
<hHash>, <nPosition>
) β xKey
Retrieves the key at a given position in a hash table. The<nPosition>
value must be in the range1...Len( hHash )
. If hash table is empty or<nPosition>
is zero or greater thanLen( hHash )
then a"Bound error"
RTE (runtime error) occurs . -
hb_HKeys(
<hHash>
) β aKeys
returns an array with all the keys (but not the associated values) of a hash table. -
hb_HMerge(
<hsDestination>, <hsSource>, <bBlock>|<nMethod>
) β hsDestination
Merges<hsSource>
hash into<hsDestination>
hash. With the 3rd parameter can be optionally specified the merge mode. When source elements copied will substitute those with the same key in the destination hash table. -
hb_HPairAt(
<hHash>, <nPosition>
) β aKeyValue
returns a two-dimensional array of thekey/value
pair entry of the hash table. -
hb_HPos(
<hHash>, <Key>
) β nPosition
returns the position of<xKey>
in the hash. -
hb_HScan(
<hHash>, <uValue>|<bBlock> [, <nStart>, <nCount>], <lExact>
) β nPosition
Scans the hash table values searching for<uValue>
, or you can pass a codeblock<bBlock>
that receives the key, the value and the position of each element, and should return.T.
if a match is found. The range of hash table items to be scanned can be limited by the<nStart>
and<nCount>
parameters.
The logical flag<lExact>
determines if exact matching (or not) will be applied when searching for strings or datetime or array or hashes. -
hb_HSet(
<hHash>, <xKey>, <xValue>
) β hHash
Sets or adds a key/value pair into<hHash>
hash table as follows:- If the
<xKey>
exists then its value is replaced by<xValue>
, the length of hash remains unchanged. - If the
<xKey>
does not exist then a new pair (key,value) is added to<hHash>
, hence it's length increases by one (in this case, function is similar toAAdd()
function used on arrays).
- If the
-
hb_HSort(
<hHash>
) β hHash
Marks<hHash>
hash for sorting.BINARY
andCASEMATCH
flags will affect the result. -
hb_HValueAt(
<hHash>, <nPosition>, [<xNewValue>]
) β
Returns the value associated to the key at a given position in a hash table. Optionally, you can set the new value<xNewValue>
associated to the key at that position. -
hb_HValues(
<hHash>
) β aValues
returns an array with all the associated values (but not the keys) of a hash table.
Note: For detailed documentation about Harbour hashes see also: Juan Gamero's site