develooper Front page | perl.perl6.users | Postings from July 2017

clone and deep copy of arrays

Thread Next
From:
Gabor Szabo
Date:
July 11, 2017 10:27
Subject:
clone and deep copy of arrays
Message ID:
CABe4FJB+WKwURGMaPWTD7hKqxzV1774HK_Ho5wkiAm=UB3b=Uw@mail.gmail.com
Hi,

I wonder what does .clone do and how can I create a deep copy or deep
clone of an array?

Reading https://docs.perl6.org/type/Array#method_clone I don't
understand what does it do as the example there works without clone as
well:

suggest I'd need a

> my @a = (1, 2, 3)
[1 2 3]
> my @b = @a;
[1 2 3]
> @a[1] = 42
42
> @b.append(100)
[1 2 3 100]
> dd @a
Array @a = [1, 42, 3]
Nil
> dd @b
Array @b = [1, 2, 3, 100]
Nil


On the other hand if I have a deeper data structure, then even .clone
does not help.


> my @x = {a => 1}, {b => 2};
[{a => 1} {b => 2}]
> my @y = @x.clone
[{a => 1} {b => 2}]
> @x[0]<a> = 42
42
> dd @x
Array @x = [{:a(42)}, {:b(2)}]
Nil
> dd @y
Array @y = [{:a(42)}, {:b(2)}]
Nil

So I wonder what does .clone do and how could I make a deep copy (deep
clone) of the whole data structure I have in @x.

regards
   Gabor

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About