Avatar of nikif
nikif
Flag for Greece asked on

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
Nikiforos
Delphi

Avatar of undefined
Last Comment
nikif

8/22/2022 - Mon
MerijnB

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;
begin
 MyList := TList.Create();
 TempList := TList.Create();
 
 try
  // 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
 finally
  MyList.Free();
  TempList.Free();
 end;
end;

Open in new window

nikif

ASKER
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
Nikiforos
ASKER CERTIFIED SOLUTION
MerijnB

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
nikif

ASKER
That's perfect
Thank you
Nikif
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
nikif

ASKER
That's perfect

Thank you
Nikiforos