RosettaCodeData/Task/Search-a-list/R/search-a-list-1.r
2023-07-01 13:44:08 -04:00

6 lines
249 B
R

find.needle <- function(haystack, needle="needle", return.last.index.too=FALSE)
{
indices <- which(haystack %in% needle)
if(length(indices)==0) stop("no needles in the haystack")
if(return.last.index.too) range(indices) else min(indices)
}