We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Copy dynamic array

so3
so3 asked
on
Medium Priority
409 Views
Last Modified: 2010-04-05
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.

Comment
Watch Question

Commented:
try to declare
Procedure RebuildFilesArray( VAR  OldFiles: array of TFiles; NrFiles:Integer);
 
Lukasz Zielinskisoftware developer
CERTIFIED EXPERT
Top Expert 2008
Commented:
reset OldFiles with
SetLength(OldFIles,0);
SetLength(OldFiles,nr_new);

moreover use Move() instead for to do loop.

ziolko.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
so3

Author

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


ziolko,
how can i use move(). Can you give me an example please?
Lukasz Zielinskisoftware developer
CERTIFIED EXPERT
Top Expert 2008

Commented:
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.
ZhaawZSoftware Developer
Commented:
btw, nr_new in Move() procedure should be "length(OldFiles) * sizeof(TFiles)"
Lukasz Zielinskisoftware developer
CERTIFIED EXPERT
Top Expert 2008

Commented:
ZhaawZ indeed :)

ziolko.
so3

Author

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

Commented:
have You tried my solution?
so3

Author

Commented:
yes i've tried your suggestion to but still when exting the content of tfiles is the same

Commented:
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;
Commented:
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;

Commented:
I counted on my fingers, instead of in my head, and it should be -

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

sorry,
so3

Author

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.

Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.