Re: [If/then and sequences...]

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

On Wed, 30 Aug 2000 18:06:35 -0700, Hawke' wrote:

>and match_all() might be a wee tougher to code, and
>i dont have time at this point to hack it out...

That's where the power of recursion comes in handy.  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 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 {}

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

Search



Quick Links

User menu

Not signed in.

Misc Menu