Recent

Author Topic: copy function  (Read 10371 times)

acp693

  • Jr. Member
  • **
  • Posts: 73
copy function
« on: November 20, 2009, 09:31:28 am »
Hello,

I declare two dynamic arrays, the following code snippet copies the third column of aArray into the third column of bArray.
What's the syntax for copying a row?

setlength (AArray,3,3);
..  // fill aArray with values
..
setlength (bArray,3,3);
bArray[2]:= copy(aArray[2]);


Also, Is it possible to copy more than one row or column at a time or do I have to loop through the columns and copy each time?

Best regards
Albert

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12195
  • Debugger - SynEdit - and more
    • wiki
Re: copy function
« Reply #1 on: November 20, 2009, 09:42:09 am »
You will have to loop. Or (for multi row, if consecutive) you need to declare the array as one dimensional, and simulate the dimensions yourself:

  setlength (AArray, RowCount * ColCount);
  x := AArray[Araow * RowCount + ACol);

For columns to be copied, you need to write a loop too.

--------
For explanation

  setlength (AArray,3,3);

creates 4 arrays, each in a different location in memory.
- It creates 3 arrays for each of the rows
- it creates one array, to hold the 3 arrays above

There is no array representing a column, so you can't use copy.

acp693

  • Jr. Member
  • **
  • Posts: 73
Re: copy function
« Reply #2 on: November 20, 2009, 09:54:19 am »
Thanks, but I'm confused, the above code copies a column from aArray into a column of bArray and not a row. Or have I misunderstood what you are saying?

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1933
Re: copy function
« Reply #3 on: November 20, 2009, 10:39:10 am »
Code: [Select]
// Set the length of the 1st dimension of the multi-dim array
  SetLength(multiArray, 3);

  // Set the length of the 3 sub-arrays to different sizes
  SetLength(multiArray[0], 1);
  SetLength(multiArray[1], 2);
  SetLength(multiArray[2], 3);

from:
http://www.delphibasics.co.uk/RTL.asp?Name=Array

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12195
  • Debugger - SynEdit - and more
    • wiki
Re: copy function
« Reply #4 on: November 20, 2009, 11:32:15 am »
Thanks, but I'm confused, the above code copies a column from aArray into a column of bArray and not a row. Or have I misunderstood what you are saying?

you misunderstood.

there is no copy for columns. only do able by loop.

if you want to copy 2 consecutive rows , you normally need to calls to copy (so you may end up with a loop)

Anyway, copy will internally run a loop too.

But if you want to copy row n and row n+1 with a single copy command, then you must declare your array, to have a single index.

This single index you can calculate as shown above.

setlength(a, 3, 4); a[y, x] := 1;
setlength(a, 12);   a[y*4+x] := 1;

both gives you 12 slots. but in the 2nd example, you can copy any subset of the array, so you can specify you want a copy from the begining of row 1 to the end of row 2.



acp693

  • Jr. Member
  • **
  • Posts: 73
Re: copy function
« Reply #5 on: November 20, 2009, 11:38:32 am »
Yes, the code you've shown sizes each column to different lengths. Not each row.
 I was confused by Martin_fr's comment:  "There is no array representing a column, so you can't use copy." The code shown by Theo represents arrays as columns.

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1933
Re: copy function
« Reply #6 on: November 20, 2009, 02:02:52 pm »
Theo represents arrays as columns.

Well, it's a question of how you look at it.

You can say multiArray (in the example) is an array of rows or an array of columns,
where multiArray[0] etc. can be seen as row or column.

There's no difference. But if you look at it as columns then you can't copy rows as a whole and vice versa.

acp693

  • Jr. Member
  • **
  • Posts: 73
Re: copy function
« Reply #7 on: November 20, 2009, 02:37:09 pm »
Theo, I don't think that's strictly true, I'm not trying to be obnoxious or anything, I just want to undesrstand this correctly.

Taking the example from the page you gave http://www.delphibasics.co.uk/RTL.asp?Name=Array

he does the following:

SetLength(multiArray[0], 1);
SetLength(multiArray[1], 2);
SetLength(multiArray[2], 3);

Look at the results in the green box lower down the page

multiArray[0,0] = 0
multiArray[1,0] = 1
multiArray[1,1] = 2
multiArray[2,0] = 2
multiArray[2,1] = 3
multiArray[2,2] = 4

the first number in those array references is the column index, the second number the row index, so the following can be said about the sizes

"Column 0" is 1 element long,  "column 1" is 2 elements long and "column 2" is 3 elements long

if you wanted to look at the sizes of the rows, then "row 1" is  3 elements long, "row 2" is 2 elements long and "row three is 1 elements long. this  doesn't fit with the original array declarations, whereas looking it as arrays of columns does.

Regards

Albert

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1933
Re: copy function
« Reply #8 on: November 20, 2009, 02:56:32 pm »
I don't understand.

Imho you can look at it like this:
If you rename multiArray to Columns then it reads like
Columns[ColIndex,Rowindex];

If you rename multiArray to Rows then it's
Rows[RowIndex,ColIndex]

There's no technical difference afaics.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12195
  • Debugger - SynEdit - and more
    • wiki
Re: copy function
« Reply #9 on: November 20, 2009, 03:02:20 pm »
just because one example uses
  array[row, col]
doesn't mean it is the only way.

It is up to each programmer do use an array as [col, row] or [row, col].

From the compilers point of view that makes no difference.

In fact, there is no such thing as rows and columns. That is only an interpretation of the programmer, and in some cases a way to represent the output.

It's a 2 dimensional array, it has a first and a second dimension.

Instead of row column, the may be hours and minutes; time and distance; direction and length; ....

And neither of them dictate an order. Perfectly valid code:
 Setlength(Something, 60, 24);
 Something[Minutes, Hours];


--
But I grant you your initial example was [colum, row] and I should have stuck to it.

acp693

  • Jr. Member
  • **
  • Posts: 73
Re: copy function
« Reply #10 on: November 20, 2009, 03:19:13 pm »
Okay, but when it comes to using components like stringGrid for example, then the first index is the column index and the second index is the row index. Given that, it makes sense to work with the first index as a column index when using arrays.  I'm fairly new to pascal, but have used Fortran for many years, In Fortran the first index of a multidimensional array is always regarded as the row index. I just think some kind of standardised method is a good thing, especially when one calls external libraries, then, it's extremely important to get the column/row ordering correct in the variables passed to those libraries.

Best regards

Albert

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12195
  • Debugger - SynEdit - and more
    • wiki
Re: copy function
« Reply #11 on: November 20, 2009, 03:33:29 pm »
True. also true if you work in a team etc.

Anyway this thread went far from were it started...

point is, if you use [col, row] then you can use copy for a row, but not a col
and if you use [row, col] then it's the other way round


 

TinyPortal © 2005-2018