• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 393
  • Last Modified:

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.

0
so3
Asked:
so3
  • 4
  • 3
  • 3
  • +2
3 Solutions
 
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 3
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now