Join 2 lists

From Director Online Wiki
Jump to: navigation, search

Copy and paste on a movie script:


on joinLists l1, l2 -- linear or property lists
  --------------------------------------
  -- joins two linear or property lists (not destructive)
  -- returns a property list if both are property lists. Otherwise returns a linear list
  -- if duplicated props exist, they will also be duplicated in result
  -- by Iñigo de Gracia
  --------------------------------------
 
  if not (listp (l1) and listp (l2)) then return #error --or void, or whatever you prefer
 
  ilk1 = ilk (l1)
  ilk2 = ilk (l2)
 
  if ilk1 = #propList and ilk2 = #propList then-- both are property lists
 
    ll = l1.duplicate()
    l2Count = l2.count
    repeat with i = 1 to l2Count
      ll.addProp (l2.getPropAt (i), l2[i])
    end repeat
 
  else -- at least one is a linear list
 
    if ilk1 = #propList then 
      ll = []
      l1Count = l1.count
      repeat with i = 1 to l1Count
        ll.add (l1[i])
      end repeat
    else
      ll = l1.duplicate()
    end if 
    -- ll is a linear list
    l2Count =l2.count()
    repeat with i = 1 to l2Count
      ll.add (l2[i])
    end repeat
 
  end if
 
  return ll
end