copy

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

copy

Andreas Zieritz-2
This should really be 'basic stuff', I know...

When I write

a = 1
b = a

b is more or less a pointer to a, it is not a copy. So if I change a, the
value of b changes, too.

What if I want a 'copy' of a? (I need that when copying an array).

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To unsubscribe from this mailing list ( ibm-netrexx ), please send a note to
[hidden email]
with the following message in the body of the note
unsubscribe ibm-netrexx <e-mail address>

Reply | Threaded
Open this post in threaded view
|

Re: copy

kikuti
Andreas Zieritz wrote:

>
> This should really be 'basic stuff', I know...
>
> When I write
>
> a = 1
> b = a
>
> b is more or less a pointer to a, it is not a copy. So if I change a, the
> value of b changes, too.
>
> What if I want a 'copy' of a? (I need that when copying an array).
>

Do those work like that?

I tried:

a = 1
b = a

say a b

a=2
say a b

And the result was:


1 1
2 1


So it looks the copy is there.


kikuti

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To unsubscribe from this mailing list ( ibm-netrexx ), please send a note to
[hidden email]
with the following message in the body of the note
unsubscribe ibm-netrexx <e-mail address>

Reply | Threaded
Open this post in threaded view
|

Re: copy

Andreas Zieritz-2
>Do those work like that?
>
>I tried:
>
>a = 1
>b = a
>
>say a b
>
>a=2
>say a b
>
>And the result was:
>
>
>1 1
>2 1
>
>
>So it looks the copy is there.
>
>
>kikuti

You're definitly right! I should have tried it out before I scare other
people. But now I HAVE tried it and what I found out was the following:

/* test.nrx */
a = 1
b = a
say a b

a = 2
say a b


c = int[1,1]
c[0,0] = 1
d = a
say c[0,0] d[0,0]

c[0,0]=2
say c[0,0] d[0,0]

-----
result:
1 1
2 1
1 1
2 2

The behaviour of 'normal' variables and arrays seems to be different! This
is - in my opinion - quite astonishing.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To unsubscribe from this mailing list ( ibm-netrexx ), please send a note to
[hidden email]
with the following message in the body of the note
unsubscribe ibm-netrexx <e-mail address>

Reply | Threaded
Open this post in threaded view
|

Re: copy

Martin Lafaix
In reply to this post by Andreas Zieritz-2

> From: "Andreas Zieritz" <[hidden email]>
> The behaviour of 'normal' variables and arrays seems to be different! This
> is - in my opinion - quite astonishing.

Here is an email explaining this behavior I sent to kikuchi
earlier today (I thought I sent it to the mailing-list.  My
mistake).

A little addenda has been added to handle your 'discovery' :-)

---snip

> From: "kikuchi" <[hidden email]> > Do those work like that?
>
> I tried:
>
> a = 1
> b = a
>
> say a b
>
> a=2
> say a b
>
> And the result was:
>
>
> 1 1
> 2 1
>
>
> So it looks the copy is there.

Well, no, in this case it's not copy.  You are assigning a new
object to a.  You don't change the value of it.

A little ascii drawing to explain it:
                                         somewhere in memory
                                               +-+  +-+
a = 1                            a ----------->|1|  |2|
                                               +-+  +-+
                                                ^    ^
                                                |    |
b = a                            b -------------+    |
                                                     |
a = 2                            a ------------------+

The first statement makes a refer to the '1' object;

The second statement makes b refer to the object refered by a
(i.e., the '1' object);

The last statement makes a refer to the '2' object.  But it does
not modify the b object, and hence b still refer to the '1'
object.

That's why a copy operation is not needed when dealing with
immutable objects such as Rexx strings.

Also, please note that assignment does not modify an object.
---snip

Addendum:

Arrays are mutable objects.  The '=' syntax you use to modify them
is not assignment.  When you write 'c[4] = 4' for example, you
are modifying the object referred by c.

That is, the following two statements are semantically different:

a = int[5]

a[2] = 4

The first one is an assignment.  The second one is not (you have
a compound term on the left of the '=' sign).


Martin, ascii artist wanabe
--
[hidden email]
Team OS/2
http://www.mygale.org/~lafaix

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To unsubscribe from this mailing list ( ibm-netrexx ), please send a note to
[hidden email]
with the following message in the body of the note
unsubscribe ibm-netrexx <e-mail address>

Reply | Threaded
Open this post in threaded view
|

Re: copy

kikuti
In reply to this post by Andreas Zieritz-2
Andreas Zieritz wrote:
>
> >
> >So it looks the copy is there.
> >
> >
> >kikuti
>
> You're definitly right! I should have tried it out before I scare other

I was Wrong!  Just ignore my comments.
I am still a learning boy.

kikuti

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To unsubscribe from this mailing list ( ibm-netrexx ), please send a note to
[hidden email]
with the following message in the body of the note
unsubscribe ibm-netrexx <e-mail address>

Reply | Threaded
Open this post in threaded view
|

Re: copy

Andreas Zieritz-2
In reply to this post by Andreas Zieritz-2
>Surely the only 'oddity' in your code fragment, compared at least to
>traditional programming is the fact that you suddenly change 'd' from a
>simple integer (value 2 (as from d=a when a has been set to 2 earlier)) to
>being an array -- without any warning message being issued by the compiler
>or run-time executor. "Scott Pollard" <[hidden email]>


Gosh, if I would build my houses like I'm programming I'd REALLY got a
problem (I'm a civil engineer, not a computer expert - obviously)!
Hopefully this time I'm doing better...

c = int[1,1]
c[0,0] = 1
d = c
say c[0,0] d[0,0]

c[0,0]=2
say c[0,0] d[0,0]

and the answer is...
1 1
2 2

So - no changes here...

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To unsubscribe from this mailing list ( ibm-netrexx ), please send a note to
[hidden email]
with the following message in the body of the note
unsubscribe ibm-netrexx <e-mail address>

Reply | Threaded
Open this post in threaded view
|

Re: copy

Andreas Zieritz-2
In reply to this post by Andreas Zieritz-2

At 12:37 05.05.98 +0200, Pierantonio Marchesini wrote:
>  That's how you do what you want to do.

After what Martin Lafaix wrote the only thing I need is...

                        b = a.clone

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To unsubscribe from this mailing list ( ibm-netrexx ), please send a note to
[hidden email]
with the following message in the body of the note
unsubscribe ibm-netrexx <e-mail address>

Reply | Threaded
Open this post in threaded view
|

Re: copy

Andreas Zieritz-2
In reply to this post by Andreas Zieritz-2
At 12:27 06.05.98 +0200, Pierantonio Marchesini wrote:

>Hi:
>
>  oops, I saw that you actually sent my reply to the list, with the
>"clone" solution, very much shorter than mine, indeed.
>
>  But, as far as I tried, I could not get that working with int[,]
>arrays, which was in your original post
>
>  Can you please show me how you did? Just send back (to me, not necessarily
>to the list) ate.nrx using "m1.clone" instead of the copy().
>I'm suspecting something in my configuration.


Well, actually I didn't get it work. I think we have to ask Martin Lafaix.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To unsubscribe from this mailing list ( ibm-netrexx ), please send a note to
[hidden email]
with the following message in the body of the note
unsubscribe ibm-netrexx <e-mail address>

Reply | Threaded
Open this post in threaded view
|

Re: copy

Martin Lafaix
In reply to this post by Andreas Zieritz-2

> From: "Andreas Zieritz" <[hidden email]>
> At 12:27 06.05.98 +0200, Pierantonio Marchesini wrote:
> >Hi:
> >
> >  oops, I saw that you actually sent my reply to the list, with the
> >"clone" solution, very much shorter than mine, indeed.
> >
> >  But, as far as I tried, I could not get that working with int[,]
> >arrays, which was in your original post
> >
> >  Can you please show me how you did? Just send back (to me, not necessarily
> >to the list) ate.nrx using "m1.clone" instead of the copy().
> >I'm suspecting something in my configuration.
>
> Well, actually I didn't get it work. I think we have to ask Martin Lafaix.

Well, two things here.

1- There is no such thing as a multi-dimensional arrays in Java.
   Multi-dimensional arrays are in fact arrays of arrays.  (And
   arrays are mutable objects.)

2- The default clone method performs a surface-level copy of the
   object.  This is often sufficient.  For example, with an array
   of int, it produces the expected result:

   Sample:
 
     -- clonearray.nrx
   
     a = [int 1, 2, 3, 4]
     b = int[] a.clone

     a[0] = 10
     b[1] = 20
   
     say 'a = ['a[0]', 'a[1]', 'a[2]', 'a[3]']'
     say 'b = ['b[0]', 'b[1]', 'b[2]', 'b[3]']'
 
   Output:
 
     a = [10, 2, 3, 4]
     b = [1, 20, 3, 4]


Bottom line, when dealing with arrays of arrays, it does not give
the result you are waiting for:

Sample:

  -- clonearray.nrx

  a = [[int 1, 2], [int 3, 4]]
  b = int[,] a.clone

  a[0,0] = 10
  b[1,1] = 20

  say 'a =' a[0,0] a[0,1] a[1,0] a[1,1]
  say 'b =' b[0,0] b[0,1] b[1,0] b[1,1]

Output:

  a = 10 2 3 20
  b = 10 2 3 20

Why?  Well, it's time for some more ascii art:

                                 somewhere in memory
                               +---------------------v
                           +---|-+      +------+   +------+
a = [[1, 2], [3, 4]]   a ->|[+,+]|      |[1, 2]|   |[3, 4]|
                           +-|---+      +------+   +------+
                             +------------^ ^        ^
                                            |        |
                             +--------------+        |
                             | +---------------------+
                           +-|-|-+
b = int[,] a.clone     b ->|[+,+]|
                           +-----+

The first statement creates an array of two elements, one referring
to [1, 2] while the other refers to [3, 4].

The second statement creates a new array of two elements.  But
those elements refer to the same objects as those contained in a
(that is, the clone method just copy the surface-level elements).

When you modify one of those elements, both a and b are affected.

Sometime, it's what you want, but sometime it's not.

If what you are looking for is a deeper copy, you have to do it by
yourself.

For example, for two-dimensional arrays of int, you can use:

  -- clonearray.nrx

  a = [[int 1, 2], [int 3, 4]]
  b = cloneArrayInt2D(a)

  a[0,0] = 10
  b[1,1] = 20

  say 'a =' a[0,0] a[0,1] a[1,0] a[1,1]
  say 'b =' b[0,0] b[0,1] b[1,0] b[1,1]

  method cloneArrayInt2D(source = int[,]) returns int[,] static signals CloneNotSupportedException
    result = int[source.length, 0]
    loop i = 0 for source.length
      result[i] = int[] source[i].clone
    end
    return result

It gives the result you are expecting.  (The cloneArrayInt2D
method contains a kludge, the null last dimension.  Idealy, only
the first dimension is needed, but doing so is not allowed by
NetRexx yet.  More work for Mike after the completion of his
worldwide tour :-)

If you are often dealing with multidimensional arrays of mixed
types and dimensions, writing a cloneArrayXxx method for each
case can quickly become boring.  You can then consider writing a
generic deep-copy method for all kind of arrays (see the Array
class for help).

Hope this helps.


Martin
--
[hidden email]
Team OS/2
http://www.mygale.org/~lafaix

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To unsubscribe from this mailing list ( ibm-netrexx ), please send a note to
[hidden email]
with the following message in the body of the note
unsubscribe ibm-netrexx <e-mail address>