fluffos/docs/efun/arrays/member_array.md
Claude 75091675af core: member_array() flag 4 runs a function argument as a predicate
member_array(fp, arr, start, 4) now calls fp on each element and
returns the first index where it yields truthy, composing with the
existing prefix (1) and reverse (2) flag bits; without flag 4 a
function argument still matches by pointer identity as before.

Also fixes an adjacent bug: a reverse search (flag 2) that found
nothing returned sizeof(arr) instead of -1, because the found-index
mirroring was applied to the failure sentinel too.

Fixes #900

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016FMBJLkpkpVpZdz6PWzeWe
2026-07-10 10:26:58 -04:00

1.6 KiB

title
arrays / member_array

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 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)