On 22/7/04 10:32 pm, "Matt Wells" <[EMAIL PROTECTED]> wrote:
> My two lists:
> 
> List1 = ["<text align="right">": 0, "<text align="left">": 10]
> List2 = ["</text>": 10, </text>": 19]
> 
> What I need is:
> 
> List3 = ["<text align="right">": 0, "</text><text align="left">":10,
> "</text>": 19]

Hi Matt,

My solution is similar to Irv's.  Differences are:
+ The structure of the input lists is the same as yours
+ You have the option of outputting with the same structure
+ </closing tag> items will always be placed first
+ Output will be ordered by item number, regardless of the
  order if items in the input
- Irv's code is shorter.

Cheers,

James

----------------------------------------------------------------------------

on test
  list1 = [:]
  addProp(list1, "<text align="&QUOTE&"left"&QUOTE&">", 10)
  addProp(list1, "<text align="&QUOTE&"right"&QUOTE&">", 0)
  put combineLists(list1, ["</text>": 19, "</text>": 10])
end

-- [0: "<text align="right">", 10: "</text><text align="left">", 19:
"</text>"]


on combineLists(aList1, aList2)
  tOutput = [:] -- will contain [<itemNumber>: </combined><tag>, ...]
  sort(tOutput) -- so that itemNumbers appear in ascending order
  
  aList1  = duplicate(aList1)
  aList2  = duplicate(aList2)
  
  tCount  = count(aList1) -- assume list 1 is shorter
  tCount2 = count(aList2)
  
  
  if tCount > tCount2 then
    -- List 2 is in fact shorter.  Swap lists so list 1 is shorter.
    tCount = tCount2
    tTemp  = aList2
    aList2 = aList1
    aList1 = tTemp
  end if
  
  
  -- aList1 is now the shorter list, and tCount is the number of items
  -- in it.
  repeat while tCount
    -- Copy the details of the last item in the list, then delete it
    tItemNumber = getAt(aList1, tCount)
    tTag        = getPropAt(aList1, tCount)
    aList1.deleteAt(tCount)
    
    -- Check if aList2 contains an entry with the same item number
    tMatch = getPos(aList2, tItemNumber)
    if tMatch then
      -- Concatenate the items, ensuring that a </xxx> tag appears
      -- first.
      tTag2 = getPropAt(aList2, tMatch)
      
      if tTag2 starts "</" then
        tTag  = tTag2&tTag
      else
        put tTag2 after tTag
      end if
      
      addProp(tOutput, tItemNumber, tTag)
      
      -- Delete the treated item from the longer list
      aList2.deleteAt(tMatch)
      
    else
      -- There is no match for the given item number
      addProp(tOutput, tItemNumber, tTag)
    end if
    
    tCount = tCount - 1
  end repeat
  
  -- aList2 will now contain only unmatched items.  Copy these to the
  -- output list.
  tCount = count(aList2)
  repeat while tCount
    tItemNumber = getAt(aList2, tCount)
    tTag        = getPropAt(aList2, tCount)
    
    addProp(tOutput, tItemNumber, tTag)
    
    tCount = tCount - 1
  end repeat
  
  --  -- Invert properties and values in tOutput, if required
  --  tTemp = tOutput
  --  tOutput = [:]
  --  tCount = count(tTemp)
  --  repeat with i = 1 to tCount
  --    tItemNumber = getAt(tTemp, i)
  --    tTag        = getPropAt(tTemp, i)
  --    
  --    addProp(tOutput, tItemNumber, tTag)
  --  end repeat
  
  return tOutput
end combineLists

[To remove yourself from this list, or to change to digest mode, go to 
http://www.penworks.com/lingo-l.cgi  To post messages to the list, email [EMAIL 
PROTECTED]  (Problems, email [EMAIL PROTECTED]). Lingo-L is for learning and helping 
with programming Lingo.  Thanks!]

Reply via email to