Moving a list into another

Good morning,
could anyone help me in this?

In Delphi-6 I have a piece of code which declares
var MyList, TempList: TList;

MyList has some items. TempList has the same items in a different order. Both lists refer to pointers of the same type.
How can I move TempList to the var MyList? I do not wish to execute re-ordering directly in MyList since it might no be definitive.

When I assign
MyList := TempList;
all looks fine, although freeing lists produces following error
"Access violation at address [addr]. Read of address [addr]"

Thank you for the attention
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MerijnBSr. Software EngineerCommented:
The reason you get the access violation, is (at least I assume) that you free TempList.
When you do MyList := TempList, both vars point to the same list, if you that list, both variables become invalid.

try something like:

var MyList: TList;
    TempList: TList;
    i: integer;
 MyList := TList.Create();
 TempList := TList.Create();
  // add your stuff to the lists
  // now we do the actual moving from TempList to MyList
  for i := 0 to TempList.Count - 1 do
   if MyList.IndexOf(TempList[i]) = -1 then  // check if MyList does not (already) contain this pointer in TempList
    MyList.Add(TempList[i]);                 // if not add it to MyList
  // now all pointers which were not (yet) in MyList were added from TempList
  // if you want to remove them from TempList call TempList.Clear();
  // TempList.Clear();  // uncomment this line if you want to remove the pointers from TempList

Open in new window

nikifAuthor Commented:
Dear MerijnB thank you for your attention,

Unfortunately what you proposed does not work as I expected.
Let's say MyList contains items A-B-C-D-E-F-G
TempList contains items A-F-E-D-C-B-G

Your proposition copies all pointers to MyList, so in line 18 MyList comes with items A-B-C-D-E-F-G-A-F-E-D-C-B-G
My aim is MyList with items A-F-E-D-C-B-G

Thank you
MerijnBSr. Software EngineerCommented:
Hi nikif,

Do I understand correctly that MyList should be exactly like TempList, all items which were in MyList can be discarded?

If yes, you can simply do MyList.Assign(TempList), and forget about all other code I posted.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nikifAuthor Commented:
That's perfect
Thank you
nikifAuthor Commented:
That's perfect

Thank you
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.