3
在perl6中,我想將一個數組分配給另一個數組,並讓結果數組成爲不同的實體,但似乎既不能直接分配,也不能克隆可以做我想要的。有沒有辦法將數組複製到一個表達式而不是編寫循環例程?perl6數組賦值:指針還是副本?
To exit type 'exit' or '^D'
> my @a=<a b c d e>
[a b c d e]
> my @b = <1 2 3 4 5 6 7>
[1 2 3 4 5 6 7]
> my @c = @a
[a b c d e]
> @c[3]
d
> @c[3]=3;
3
> @c
[a b c 3 e]
> @a
[a b c d e]
> @c === @a
False
> @c == @a
True # this is unexpected, @c and @a should be different, right?
> my @[email protected]
[a b c d e]
> @x[3]=3
3
> @x
[a b c 3 e]
> @x === @a
False
> @x == @a
True # unexpected, @x and @a should be distinct things, right?
>
非常感謝!
lisprog
謝謝馬特!所以,當我將一個數組分配給另一個時,內容被複制並且它們是不同的數組,對嗎? – lisprogtor
是和不是。你會得到一個標量類型的淺拷貝。所以像容器這樣的東西說另一個數組,你只能得到一個指向同一個列表的新容器。所以一個例子可能不是你所期望的:'my @a = [1,[2,3]];我的@b = @a; @b [1] [0] = 4;'這會在@a中將2更改爲4! –
如果你想要一個深層複製,那麼這已經在以前回答這裏http://stackoverflow.com/questions/38582873/does-perl-6-have-a-built-in-tool-to-make-a-deep-拷貝對的一嵌套的數據結構 –