is among - problem

Cubist at aol.com Cubist at aol.com
Sat Nov 22 04:14:45 CST 2003


   I'm a bit surprised that no one has brought up the REPLACE command in this 
context. "replace WhateverText with empty in WhateverList" will *erase* all 
instances of WhateverText from WhateverList in one fell swoop -- no need to 
bother with silly repeat loops. Since the original problem specified that we're 
dealing with 3-digit numbers, and that we want to nuke all permutations of the 
given number, here is some code which should preserve the first instance of 
whatever set of 3 digits, and nuke all the other instances as well as nuking all 
the permutations of those 3 digits.

================

  put return after DerList -- you'll see why later
  put line 1 of DerList into Fred

  -- build list of permutations
  put Fred into George
  put "," & char 1 of Fred & char 3 of Fred & char 2 of Fred after George
  put "," & char 2 of Fred & char 1 of Fred & char 3 of Fred after George
  put "," & char 2 of Fred & char 3 of Fred & char 1 of Fred after George
  put "," & char 3 of Fred & char 1 of Fred & char 2 of Fred after George
  put "," & char 3 of Fred & char 2 of Fred & char 1 of Fred after George

  -- eliminate duplicates from George
  repeat with K1 = 6 down to 2 step -1
    if item K1 of George is in item 1 to (K1 - 1) of George then delete item 
K1 of George
  end repeat

  if the number of items in George > 1 then
    -- use the REPLACE trick!
    repeat with K1 = 2 to (the number of items in George)
      replace (item K1 of George & return) with empty in George
      -- now d'you see why i appended that return at the start of this code?
    end repeat
  end if

  -- find the first line which contains Fred
  put the number of lines in char 1 to (offset (Fred,DerList)) of DerList 
into DisLine

  -- nuke all dupes of Fred
  put line (DisLine + 1) to (the number of lines in DerList) into Zelda
  replace (Fred & return) with empty in Zelda

  -- insert the result back into the list
  put Zelda into line (DisLine + 1) to (the number of lines in DerList) of 
DerList

================

   Hope this helps...


More information about the use-livecode mailing list