Re: Find what?

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

On Mon, 6 Nov 2000 15:29:08 -0800, Thomas wrote:

>Hi evr'yone
>
>I was wondering about find(). What if there are 2 of the same characters
in a sequence? Like this:
>
>sequence word
>word="beet"
>atom findit
>
>findit=find("e", word)
>? findit
>
>Does it give a [2..3] back?
>
>-->Thanks
>--->Thomas
>-------/\====

Well this sounds like a job for 'experimentation'!!!  But since I suspect
you might already know the answer, I'll give you something more to chew
on.  Suppose your real question was something like:  "How might I get the
indices of all elements found in a sequence?" or "how about all sequences
matched?"

Here's some recursive functions I posted to the list a little while back.
Recursive functions are functions that call themselves to get the answer
you're looking for:

... These return empty sequences if there's no match but I really don't see
the advantage of returning zero since you'd have to store the result in an
object and test for type that way...

-- find_all/match_all recursive functions
--  Brian Broker
  -----------------------------
function find_all( object x, sequence s )
-- returns: sequence containing indices of all occurences
--   if unsuccessful return empty sequence {}
  object found

  found = find( x, s )
  if found then
    found &= found + find_all( x, s[found+1..length(s)] )
  else
    return {}
  end if

  return found

end function
  -----------------------------
function match_all( sequence s1, sequence s2 )
-- returns: sequence containing indices of all matches
--   if unsuccessful return empty sequence {}
  object found

  found = match( s1, s2 )
  if found then
    found &= found + match_all( s1, s2[found+1..length(s2)] )
  else
    return {}
  end if

  return found

end function
  -----------------------------

-- test the functions
constant blah = {10,20,3,1,3,4,11,15}
constant names = {"mary","john","mary","frank",""}
constant zippy = {{12,13,14,15},{11,12,13,14,15},{10,11,12,13,14}}

constant
string1 = "My name is Brian.  My name is not Thomas.  What is your name?",
string2 = "Euphoria is fun.  Euphoria is also flexible and fast."

? find_all(3,blah)
---output is {3,5}
? find_all(11,blah)
---output is {7}
? find_all(28,blah)
---output is {}
? find_all("mary",names)
---output is {1,3}
? find_all("john",names)
---output is {2}
? find_all("joebob",names)
---output is {}

---these are tricky ;)
? find(12,zippy)
---output is 0
? find_all(12,zippy)
---output is {}

? match( "name", string1 )
--output is 4
? match_all( "name", string1 )
--output is {4,23,57}

? match( "not", string1 )
--output is 31
? match_all( "not", string1 )
--output is {31}

? match( "Euphoria", string2 )
--output is 1
? match_all( "Euphoria", string2 )
--output is {1,19}

? match( "Python", string2 )
--output is 0
? match_all( "Python", string2 )
--output is {}

-- have fun,
-- Brian

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

Search



Quick Links

User menu

Not signed in.

Misc Menu