Copy dynamic array

I'm trying to make a procedure that copies a dynamic array in another but after running the procedure destination array is still the sam. How can i make it so when quiting the procedure the content of array will be the good one.

Here is the procedure  i use:


Procedure RebuildFilesArray(OldFiles: array of TFiles; NrFiles:Integer);
  var i,nr_new:integer;
  var NewFiles: array of TFiles;
begin
    nr_new:=0;
    for I := 0 to NrFiles-1 do
      begin
        if (OldFiles[i].Url<>'') and (OldFiles[i].Title<>'') then
           begin
             SetLength(NewFiles,nr_new+1);
             NewFiles[nr_new]:=OldFiles[i];
             Inc(nr_new);
           end;
      end;

    for I := 0 to nr_new - 1 do
        begin
           OldFiles[i]:=NewFiles[i];
        end;
    NewSizeArray:=nr_new;
end;


here is the code that i want to give to the new array

             NewSizeArray:=Length(baza[index].Files);
              RebuildFilesArray(baza[index].Files,NewSizeArray);
              SetLength(baza[index].Files,NewSizeArray);

After running the procedure baza[index].Files will have the same content as before.

LVL 6
so3Asked:
Who is Participating?
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.

mokuleCommented:
try to declare
Procedure RebuildFilesArray( VAR  OldFiles: array of TFiles; NrFiles:Integer);
 
0
ziolkoCommented:
reset OldFiles with
SetLength(OldFIles,0);
SetLength(OldFiles,nr_new);

moreover use Move() instead for to do loop.

ziolko.
0
so3Author Commented:
thanks,
i'll try when i arrive at home.


ziolko,
how can i use move(). Can you give me an example please?
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

ziolkoCommented:
Move(OldFiles[0],NewFiles[0],nr_new);

one more tip if TFiles is class then write descendatnt of TObjectList and add new method Rebuild() which will
remove from list items which .Url or .List is empty string.

ziolko.
0
ZhaawZSoftware DeveloperCommented:
btw, nr_new in Move() procedure should be "length(OldFiles) * sizeof(TFiles)"
0
ziolkoCommented:
ZhaawZ indeed :)

ziolko.
0
so3Author Commented:
SetLength(OldFIles,0);
SetLength(OldFiles,nr_new);

When i tried to use setlength in procedure for oldfiles i get errors like incompatible types. Outside works ok

I've also tried Move(NewFiles,OldFiles,length(NewFiles)*SizeOf(tfiles)) but is not working. I get a memory error after running the code and also it puts in oldfiles array characters as in a strem #0#0...

I try to copy to content of newfiles in oldfiles and after running the procedure to still remain in oldfiles. did i used move correctly?

Also i've tried to use this but after using i get errors in memory:
 for I := 0 to nr_new - 1 do
       Move(NewFiles[i],baza[index].Files[i],sizeof(Tfiles));

if i use the code outside the procedure - that i've post it in my question everything is ok
0
mokuleCommented:
have You tried my solution?
0
so3Author Commented:
yes i've tried your suggestion to but still when exting the content of tfiles is the same
0
Slick812Commented:
hello so3 , ,  is there are long strings in your record in your  dynamic array then , I have never been able to use the Move or CopyMemory functions to transfer a string from one array pos to another
 ( I beleive the delphi memory manager keeps track of "Where" every long string is allocated, so it can "Free" the string when it goes out of scope, so when you reduce the length of your dynamic array, it can free the string memory in the array records)

below is code for a button click, to "Compact" an array and Remove empty strings


procedure TForm1.sbut_ReArrayClick(Sender: TObject);
type
  TFiles = record
    Url, Title: String;
    end;

var
theFiles: array of TFiles;
i, re, empty1: Integer;
begin
SetLength(theFiles, 16);
for i := 0 to High(theFiles) do
  begin
  if i in[4, 6, 9] then Continue;
  theFiles[i].Url := 'www.addy'+IntToStr(i)+'.com';
  theFiles[i].Title := 'Web Site '+IntToStr(i);
  end;
// the code above just fills the array, is NOT for compacting the array

ShowMessage('Len '+IntToStr(Length(theFiles))+' |'+theFiles[6].Url);
// showmessage for empty string

// code below removes the empty strings from theFiles
empty1 := 0;
for i := 0 to High(theFiles) do
  if (theFiles[i].Url = '') and (theFiles[i].Title = '') then
    begin
    for re := i to High(theFiles)-empty1-1 do
      theFiles[re] := theFiles[re+1];
    Inc(empty1);
    end;
SetLength(theFiles, Length(theFiles)-empty1);

// code below is to see result
ShowMessage('Len '+IntToStr(Length(theFiles))+' |'+theFiles[6].Url);
end;
0
Slick812Commented:
maybe something like this ? -


type
  TFiles = record
    Url, Title: String;
    end;
  TaryFiles = Array of TFiles;

Procedure RebuildFilesArray(var aryFiles: TaryFiles);
var
i, re, empty1: Integer;
begin
empty1 := 0;
for i := 0 to High(aryFiles) do
  if (aryFiles[i].Url = '') and (aryFiles[i].Title = '') then
    begin
    for re := i to High(aryFiles)-empty1-1 do
       aryFiles[re] := aryFiles[re+1];

    Inc(empty1);
    end;
SetLength(aryFiles, Length(aryFiles)-empty1);
end;
0

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
Slick812Commented:
I counted on my fingers, instead of in my head, and it should be -

for re := i to High(aryFiles)-empty1-2 do

sorry,
0
so3Author Commented:
Thanks Slick812 but this code is what i used and is very slow, that's why i want to use another array of tfiles and copy it back because is very fast this way. But anyway i fixed this problem.

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

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.