Method search()
- Method
search
intsearch(stringhaystack,string|intneedle,int|voidstart,int|voidend)
intsearch(arrayhaystack,mixedneedle,int|voidstart,int|voidend)
mixedsearch(mappinghaystack,mixedneedle,mixed|voidstart)
mixedsearch(objecthaystack,mixedneedle,mixed|voidstart,mixed...extra_args)- Description
Search for
needleinhaystack.- Parameter
haystack Item to search in. This can be one of:
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.objectWhen
haystackis an object implementing lfun::_search(), the result of calling lfun::_search() withneedle,startand anyextra_argswill be returned.If
haystackis an object that doesn't implement lfun::_search() it is assumed to be an Iterator, and implementIterator()->index(),Iterator()->value(), andIterator()->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.- Parameter
start If the optional argument
startis present search is started at this position. This has no effect on mappings.- Parameter
end If the optional argument
endis present, the search will terminate at this position (exclusive) if not found earlier.- Returns
Returns the position of
needleinhaystackif found.If not found the returned value depends on the type of
haystack:string|array-1.mapping|IteratorobjectThe value returned by lfun::_search().
- 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()