# find

Definition: | integer i = find(object needle, sequence haystack, integer start=1) -- or -- integer i = rfind(object needle, sequence haystack, integer start=-1)
Description: | Find needle as an element of haystack. If successful, return the index of the first element of haystack that matches. If unsuccessful return 0. The start index may optionally be specified, as per example 3 below. Negative start indices are allowed, but not 0 or < -length(haystack). In contrast, indices > +length(haystack) cause no problems, yielding 0. The rfind() routine is identical but works from the end of haystack to the start. The find_from() routine, which predated optional parameters, is now deprecated. Simply use find() with the optional third parameter instead.
pwa/p2js: | Supported. Not that anyone should care, this clobbers/overrides a non-standard Window.find() method, which (as I understand it) is equivalent to the pseudocode 'Window.simulateKeyPress("")' (etc) which returns true/false and may or may not would have selected/highlighted any found content on the web page. Otherwise works as expected.
Auxillary routine: | sequence res = find_all(object needle, sequence haystack, integer start=1) returns a sequence of the indexes of all matching elements, {} if no elements match. As long as it exists, res[1] will always be non-zero and match a plain find() given the same parameters, since it uses that internally anyway.
Example 1: |
    integer location = find(11, {5, 8, 11, 2, 3})
    -- location is set to 3
Example 2: |
    sequence names = {"fred", "rob", "george", "mary", ""}
    integer location = find("mary", names)
    -- location is set to 4
Example 3: |
    integer idx = find(8, {5, 8, 11, 8, 11, 8}, 3)
    -- idx is set to 4; the matching value at [2] was obviously skipped/not
    --                  examined because a start index of 3 was specified.
    sequence idii = find_all(8, {5, 8, 11, 8, 11, 8}, 3)
    -- idii is set to {4,6}. It would be {2,4,6} without the start of 3.
Implementation: | See builtins\find.e and/or builtins\pfindall.e (both autoincludes) for details of the actual implementation.
See Also: | match, compare