Original date:2021-02-15 05:59:06 Edited by: petelomax Subject: Re: question about find function

jessedavis said...

Thanks. Somewhat clearer. find() seems to be closer to equals() as it disallows sequences that are longer but may contain the needle. The key is, I think, "item". {"needle"} is a sequence containing one item. "needle" is a sequence containing 6 items. Oh well, back to sleep. I'll surely get caught out again in a year or so.

This may help, for any object x and sequence s (Humpf, this hasn't quite ended up as clear as I thought it would...)

object x sequence s integer i 
x = '1' s = "1234"              -- experiment here 
--x = "1" s = {"1","2"}         -- (maybe like so) 
i = find(x,s) 
if i=0 then 
    printf(1,"not found\n") 
    printf(1,"s[i]=x: %t\n",s[i]=x) 
end if 
x = "23"                        -- comment this line in/out 
--x = {"2"}                     -- (or this for above maybe) 
i = match(x,s) 
if i=0 then 
    printf(1,"no match\n") 
    if atom(x) then 
        printf(1,"s[i]=x: %t\n",s[i]=x) -- same as find() case 
        printf(1,"s[i..i+length(x)-1]=x: %t\n",s[i..i+length(x)-1]=x) 
    end if 
end if 

The above is Phix: for Euphoria replace the %t with %d (and get 1/0 instead of true/false).
In particular note that when x is not an atom, find() and match() behave quite differently.
What throws people, I think, is they are quite similar when x is an atom.

Perhaps slightly simpler, consider i=match('b',"abc") vs. i=match("b","abc"), both set i to 2.
In the first case, "abc"[2]=='b', same as find, in the second it is "abc"[2..2]=="b", with find a no-show.

Not Categorized, Please Help


Quick Links

User menu

Not signed in.

Misc Menu