SRL Archive - OSR Help

A := b


Wanted

2011-02-21 19:27:12

var
A, B: TIntegerArray;
I: Integer;

begin
A := [5, 4, 3, 2, 1];
B := A;
QuickSort(B);
for I := 0 to High(A) do
WriteLn(A[I]);
end.

I do not want it to sort A, just B.

What is the best way to avoid this?

Like other than this

var
A, B: TIntegerArray;
I: Integer;

begin
A := [5, 4, 3, 2, 1];
SetLength(B, Length(A));
for I := 0 to High(A) do
B[I] := A[I];
QuickSort(B);
for I := 0 to High(A) do
WriteLn(A[I]);
end.

Train

2011-02-21 19:36:07

Only way I can think of.

var
A, B: TIntegerArray;
I: Integer;

begin
A := [5, 4, 3, 2, 1];
B := A;
SetLength(B, Length(B));
QuickSort(B);
for I := 0 to High(A) do
WriteLn(A[I]);
end.

Wanted

2011-02-21 19:38:41

From: Train

B := A;
SetLength(B, Length(B));



Not to bad actually.

nielsie95

2011-02-21 19:39:10

Here's another way for Simba:


var
A, B: TIntegerArray;
I: Integer;

begin
A := [5, 4, 3, 2, 1];
B := A + TIntegerArray([]);
QuickSort(B);
for I := 0 to High(A) do
WriteLn(A[I]);
end.

And for ATPA and TPA you have the CopyATPA and CopyTPA functions (although the above method should work too).

Train

2011-02-21 19:39:48

There was no built-in array_copy, copy, varcopy, etc. functions I could find. :\
EDIT: nielsie95's way looks promising. ;)

WT-Fakawi

2011-02-22 02:44:46

I felt really stupid not getting array_a := array_b done, forgot how strict Delphi was. Took me a couple of hours to figure out a := b in arrays is not possible…. And one Q to Wizz. This with setfightmode bugged in SRL…..cost me some hairs.

Wizzup?

2011-02-22 11:25:25

From: Train
Only way I can think of.

var
A, B: TIntegerArray;
I: Integer;

begin
A := [5, 4, 3, 2, 1];
B := A;
SetLength(B, Length(B));
QuickSort(B);
for I := 0 to High(A) do
WriteLn(A[I]);
end.


That works? Seems like quite the hack. SetLength is really silly if it works that way… At least realloc doesn't do this, so FPC does some funny stuff internally.

Just remember that all arrays are in fact references. Like classes are references. You can't expect them to copy the other on assignment. They will just point to the same data.

nielsie95

2011-02-22 11:34:13

SetLength works because it decreases the ref count of the old array. If there are still other references (in this case a), SetLength will copy the old array to the new array. Otherwise it will reallocate the memory. (that's how strings work too, btw).

The Claw

2011-02-22 11:56:03

:sasmokin::ninja:
Or just do A := B := B := B := B and forget the whole idea Icefire.

Cost me some mass and stuff.

x[Warrior]x3500

2011-02-22 12:30:46

every try linked lists? dont know how well it will work with this situation, but its a fun thing to learn if you have never used it before.

EDIT:

http://en.wikipedia.org/wiki/Linked_list

http://www.learn-programming.za.net/programming_pascal_learn14.html

Nava2

2011-02-22 12:52:44

Perhaps a memcopy function should be implemented to save this hassle and confusion.

Wizzup?

2011-02-22 15:48:58

From: Nava2
Perhaps a memcopy function should be implemented to save this hassle and confusion.


Yes, for the interpreter with hardly any pointer support! I don't see how it will save ANY confusion. The only confusion is about the := for arrays; not about how to copy them.

Bad Boy JH

2011-02-22 22:46:46

if it was TPAs I would go

B := CombineTPA(A, []);