# Help with an algorithm...

Dar Scott Consulting dsc at swcp.com
Mon Aug 5 17:24:10 EDT 2019

```Yikes! I wasn't aware of duplicate keys being a problem. How does that happen?

> On Aug 5, 2019, at 2:28 PM, Alex Tweedly via use-livecode <use-livecode at lists.runrev.com> wrote:
>
> I'm a great fan of, and user of, arrays - but we always need to be careful of losing data with duplicated keys.
>
> You haven't said that the parent names in LISTNEW are guaranteed to be unique. This simple code assumes they are - if they're not, it's easy to add a check ...  (And it also assumes the inputs are properly formatted!!)
>
> (inputs are in fields "FNew" and "FOld", output in field "FOut")
>
> on mouseUp
>    local tOld, tNew, tOut
>    local tA
>    local tLastParent
>
>    put the text of fld "FNew" into tNew
>    repeat for each line L in tNew  -- convert to array
>       if L begins with space then -- a child
>          put L &CR after tA[tLastParent]
>       else
>          put L into tLastParent
>       end if
>    end repeat
>
>    put the text of fld "FOld" into tOld
>    put empty into tLastParent
>    repeat for each line L in tOld
>       if L begins with space then -- a child
>          if (tLastParent is not empty) \
>                 AND (L is not among the lines of tA[tLastParent]) then
>             put L &CR after tA[tLastParent]
>          end if
>       else
>          if L is among the keys of tA then -- a Parent we need to deal with
>             put L into tLastParent
>          else
>             put empty into tLastParent
>          end if
>       end if
>    end repeat
>
>    -- and then collect the expanded outptu
>    local tKeys
>    put the keys of tA into tKeys
>    sort lines of tKeys
>    repeat for each line K in tKeys
>       put K &CR & tA[K] after tOut
>    end repeat
>    put tOut into fld "fOut"
> end mouseUp
>
> Alex.
>
>
> On 05/08/2019 16:53, Paul Dupuis via use-livecode wrote:
>> Today is not my coding day. I have a problem I should be able to design a solution for an am struggling. Clearly I am missing "something"
>>
>> I have 2 lists (LISTNEW and LISTOLD) of the following format:
>>
>> ParentA
>> <space>Child 1
>> <space>Child 2
>> <space>etc.
>> ParentB
>> <space>Child 1
>> <space>etc.
>> etc.
>>
>> The parents are in alphabetical sorted order, the children may not be in sorted order
>>
>> I need to hunt through LISTOLD comparing the LISTOLD Parents to the LISTNEW Parents
>>   FOR any LISTOLD Parent present in LISTNEW, check the Children of the matching Parents and add any Child for the LISTOLD Parent that is not already under its matching LISTNEW Parent
>>   FOR any LISTOLD Parent NOT in LISTNEW, I can ignore the Parent and its Children
>>
>> I can not seem to write an approach to solve this today. Does any body have some code so solve this they may be willing to share?
>>
>> _______________________________________________
>> use-livecode mailing list
>> use-livecode at lists.runrev.com
>> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
>> http://lists.runrev.com/mailman/listinfo/use-livecode
>
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
>

```