Method search()
- Method search
intsearch(stringhaystack,string|intneedle,int|voidstart)
intsearch(arrayhaystack,mixedneedle,int|voidstart)
mixedsearch(mappinghaystack,mixedneedle,mixed|voidstart)
mixedsearch(objecthaystack,mixedneedle,mixed|voidstart)- Description
Search for
needleinhaystack. Return the position ofneedleinhaystackor-1if not found.If the optional argument
startis present search is started at this position.haystackcan have any of the following types:stringWhen
haystackis a stringneedlemust be a string or an int, and the first occurrence of the string or int is returned.arrayWhen
haystackis an array,needleis compared only to one value at a time inhaystack.mappingWhen
haystackis a mapping, search() tries to find the index connected to the dataneedle. That is, it tries to lookup the mapping backwards. Ifneedleisn't present in the mapping, zero is returned, and zero_type() will return 1 for this zero.objectWhen
haystackis an object implementing lfun::_search(), the result of calling lfun::_search() withneedlewill be returned.If
haystackis an object that doesn't implement lfun::_search() it is assumed to be an Iterator, and implement Iterator()->index(), Iterator()->value(), and Iterator()->next(). search() will then start comparing elements with `==() until a match withneedleis found. Ifneedleis foundhaystackwill be advanced to the element, and the iterator index will be returned. Ifneedleis not found,haystackwill be advanced to the end (and will thus evaluate to false), and a zero with zero_type 1 will be returned.- Note
If
startis supplied to an iterator object without an lfun::_search(),haystackwill need to implement Iterator()->set_index().- Note
For mappings and object UNDEFINED will be returned when not found. In all other cases
-1will be returned when not found.- See also
indices(), values(), zero_type(), has_value(), has_prefix(), has_suffix()