Re: question about find function

new topic     » goto parent     » topic index » view thread      » older message » newer message
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") 
else 
    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") 
else 
    if atom(x) then 
        printf(1,"s[i]=x: %t\n",s[i]=x) -- same as find() case 
    else 
        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.

One golden rule is that checking whether a string is a substring of another string is always match() and never find().

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu