Skip to main content

member_array

NAME​

member_array() - returns index of an occurence of a given item in an
array or string

SYNOPSIS​

int member_array( mixed item, mixed * | string arr,
void | int start, void | int flags );

DESCRIPTION​

Returns the index of the first occurence of 'item' in array 'arr', or
the first occurence at or after 'start'. If the item is not found,
then -1 is returned.

Note, if the second argument is a string, the first parameter must be an int
representing the character you are looking for in the provided string.
The string form is a BYTE search: 'start' and the result are byte
offsets, and only a single byte (0..255) can match. When that byte
answer is not the character answer -- an ASCII 'item' found after
non-ASCII text or a CR LF (one character), or found as the start of a
longer character (a combining sequence, the LF of a CR LF), or an
'item' above 0x7F searched in text that has non-ASCII bytes -- the
driver logs a warning, once per call site. An ASCII 'item' that is not
found, or is found before any non-ASCII text, is correct as it is. To
find any Unicode code point by character index, use strsrch(str, ch)
instead.

The optional 'flags' argument is a bit field:

1 - 'item' is a string prefix: elements that start with 'item' match.
2 - search backwards from the end of the array (arrays only); the
index of the last match is returned.
4 - 'item' is a function: it is called with each element and the
first element for which it returns a truthy value matches.

EXAMPLE​

member_array( "red", ({ "red", "blue", "red", "green", "red", }) ) ;
// 0

member_array( "red", ({ "red", "blue", "red", "green", "red", }) , 1) ;
// 2

member_array( "blue", ({ "red", "blue", "red", "green", "red", }) , 3) ;
// -1

member_array( "purple", ({ "red", "blue", "red", "green", "red", }) ) ;
// -1

member_array('F', "Drink the FluffOS Kool-Aid!") ;
// 10

member_array('Z', "Drink the FluffOS Kool-Aid!") ;
// -1

member_array( (: $1 > 10 :), ({ 1, 5, 42, 77 }), 0, 4 ) ;
// 2

member_array( "red", ({ "red", "blue", "red" }), 0, 2 ) ;
// 2 (last occurrence)