Copy a string: Difference between revisions
Content added Content deleted
(add task to ARM64 assembly Raspberry Pi) |
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
||
Line 35: | Line 35: | ||
B DS CL64 b |
B DS CL64 b |
||
REFA DS A @a</lang> |
REFA DS A @a</lang> |
||
=={{header|AArch64 Assembly}}== |
=={{header|AArch64 Assembly}}== |
||
{{works with|as|Raspberry Pi 3B version Buster 64 bits}} |
{{works with|as|Raspberry Pi 3B version Buster 64 bits}} |
||
Line 101: | Line 102: | ||
.include "../includeARM64.inc" |
.include "../includeARM64.inc" |
||
</lang> |
</lang> |
||
=={{header|ABAP}}== |
=={{header|ABAP}}== |
||
<lang ABAP>data: lv_string1 type string value 'Test', |
<lang ABAP>data: lv_string1 type string value 'Test', |
||
Line 514: | Line 516: | ||
return 0; |
return 0; |
||
}</lang> |
}</lang> |
||
=={{header|C sharp|C#}}== |
|||
<lang csharp>string src = "Hello"; |
|||
string dst = src;</lang> |
|||
=={{header|C++}}== |
=={{header|C++}}== |
||
Line 526: | Line 532: | ||
std::cout << "my_copy still is " << my_copy << std::endl; |
std::cout << "my_copy still is " << my_copy << std::endl; |
||
}</lang> |
}</lang> |
||
=={{header|C sharp|C#}}== |
|||
<lang csharp>string src = "Hello"; |
|||
string dst = src;</lang> |
|||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
Line 562: | Line 564: | ||
(princ s1) |
(princ s1) |
||
(princ s2)) ; will print "Hello!!!!!"</lang> |
(princ s2)) ; will print "Hello!!!!!"</lang> |
||
=={{header|Crystal}}== |
|||
<lang crystal>s1 = "Hello" |
|||
s2 = s1</lang> |
|||
=={{header|Component Pascal}}== |
=={{header|Component Pascal}}== |
||
Line 612: | Line 610: | ||
dest:</lang> |
dest:</lang> |
||
=={{header|Crystal}}== |
|||
<lang crystal>s1 = "Hello" |
|||
s2 = s1</lang> |
|||
=={{header|D}}== |
=={{header|D}}== |
||
Line 660: | Line 662: | ||
<pre>Goodbye |
<pre>Goodbye |
||
Goodbye, World!</pre> |
Goodbye, World!</pre> |
||
=={{header|Déjà Vu}}== |
|||
In Déjà Vu, strings are immutable, |
|||
so there really isn't a good reason to copy them. |
|||
As such, no standard way of doing so is provided. |
|||
However, one can still create a copy of a string |
|||
by concatenating it with an empty string. |
|||
<lang dejavu>local :orgininal "this is the original" |
|||
local :scopy concat( original "" ) |
|||
!. scopy</lang> |
|||
{{out}} |
|||
<pre>"this is the original"</pre> |
|||
=={{header|DWScript}}== |
=={{header|DWScript}}== |
||
Line 688: | Line 678: | ||
<lang dyalect>var src = "foobar" |
<lang dyalect>var src = "foobar" |
||
var dst = src</lang> |
var dst = src</lang> |
||
=={{header|Déjà Vu}}== |
|||
In Déjà Vu, strings are immutable, |
|||
so there really isn't a good reason to copy them. |
|||
As such, no standard way of doing so is provided. |
|||
However, one can still create a copy of a string |
|||
by concatenating it with an empty string. |
|||
<lang dejavu>local :orgininal "this is the original" |
|||
local :scopy concat( original "" ) |
|||
!. scopy</lang> |
|||
{{out}} |
|||
<pre>"this is the original"</pre> |
|||
=={{header|E}}== |
=={{header|E}}== |
||
Line 789: | Line 791: | ||
<lang elixir>src = "Hello" |
<lang elixir>src = "Hello" |
||
dst = src</lang> |
dst = src</lang> |
||
=={{header|Erlang}}== |
|||
<lang erlang>Src = "Hello". |
|||
Dst = Src.</lang> |
|||
=={{header|Emacs Lisp}}== |
=={{header|Emacs Lisp}}== |
||
Line 799: | Line 797: | ||
(setq str2 str1) |
(setq str2 str1) |
||
(eq str1 str2)</lang> |
(eq str1 str2)</lang> |
||
=={{header|Erlang}}== |
|||
<lang erlang>Src = "Hello". |
|||
Dst = Src.</lang> |
|||
=={{header|Euphoria}}== |
=={{header|Euphoria}}== |
||
Line 1,078: | Line 1,080: | ||
b = "Goodbye" // b contains a copy of a's value and a will still return "Hello"</lang> |
b = "Goodbye" // b contains a copy of a's value and a will still return "Hello"</lang> |
||
=={{header|Joy}}== |
|||
<lang joy>"hello" dup</lang> |
|||
Strings are immutable. |
|||
=={{header|jq}}== |
=={{header|jq}}== |
||
Line 1,093: | Line 1,100: | ||
{{Out}} |
{{Out}} |
||
"abc" |
"abc" |
||
=={{header|Joy}}== |
|||
<lang joy>"hello" dup</lang> |
|||
Strings are immutable. |
|||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
Line 1,237: | Line 1,239: | ||
svar.print;</lang> |
svar.print;</lang> |
||
STRING_CONSTANT is immutable, STRING is not. |
STRING_CONSTANT is immutable, STRING is not. |
||
=={{header|Little}}== |
=={{header|Little}}== |
||
<lang C>string a = "A string"; |
<lang C>string a = "A string"; |
||
Line 1,318: | Line 1,321: | ||
c; |
c; |
||
"losers"</lang> |
"losers"</lang> |
||
=={{header|MAXScript}}== |
=={{header|MAXScript}}== |
||
Line 1,335: | Line 1,337: | ||
message a; % writes "hello" |
message a; % writes "hello" |
||
end</lang> |
end</lang> |
||
=={{header|MiniScript}}== |
=={{header|MiniScript}}== |
||
Line 1,559: | Line 1,560: | ||
There is no need to copy a string content as strings are immutable. If really needed : |
There is no need to copy a string content as strings are immutable. If really needed : |
||
<lang Oforth>StringBuffer new "abcde" << </lang> |
<lang Oforth>StringBuffer new "abcde" << </lang> |
||
=={{header|OxygenBasic}}== |
|||
<lang oxygenbasic> |
|||
string s, t="hello" |
|||
s=t |
|||
</lang> |
|||
=={{header|ooRexx}}== |
=={{header|ooRexx}}== |
||
Line 1,594: | Line 1,589: | ||
s1~identityhash=17587366586244 |
s1~identityhash=17587366586244 |
||
s2~identityhash=17587366588032</pre> |
s2~identityhash=17587366588032</pre> |
||
=={{header|OxygenBasic}}== |
|||
<lang oxygenbasic> |
|||
string s, t="hello" |
|||
s=t |
|||
</lang> |
|||
=={{header|PARI/GP}}== |
=={{header|PARI/GP}}== |
||
Line 1,667: | Line 1,668: | ||
$alias = 'Good evening.'; |
$alias = 'Good evening.'; |
||
print "$original\n"; # prints "Good evening."</lang> |
print "$original\n"; # prints "Good evening."</lang> |
||
=={{header|Perl 6}}== |
|||
There is no special handling needed to copy a string; just assign it to a new variable: |
|||
<lang perl6>my $original = 'Hello.'; |
|||
my $copy = $original; |
|||
say $copy; # prints "Hello." |
|||
$copy = 'Goodbye.'; |
|||
say $copy; # prints "Goodbye." |
|||
say $original; # prints "Hello."</lang> |
|||
You can also bind a new variable to an existing one so that each refers to, and can modify the same string. |
|||
<lang perl6>my $original = 'Hello.'; |
|||
my $bound := $original; |
|||
say $bound; # prints "Hello." |
|||
$bound = 'Goodbye.'; |
|||
say $bound; # prints "Goodbye." |
|||
say $original; # prints "Goodbye."</lang> |
|||
<!-- SqrtNegInf 2016-01-16 This is NYI, so until such time as it is, leaving this section commented |
|||
You can also create a read-only binding which will allow read access to the string but prevent modification except through the original variable. |
|||
<lang perl6># y $original = 'Hello.'; |
|||
#my $bound-ro ::= $original; |
|||
#say $bound-ro; # prints "Hello." |
|||
#try { |
|||
# $bound-ro = 'Runtime error!'; |
|||
# CATCH { |
|||
# say "$!"; # prints "Cannot modify readonly value" |
|||
# }; |
|||
#}; |
|||
say $bound-ro; # prints "Hello." |
|||
$original = 'Goodbye.'; |
|||
say $bound-ro; # prints "Goodbye."</lang> |
|||
--> |
|||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Line 1,754: | Line 1,720: | ||
In PostScript, |
In PostScript, |
||
<lang postscript>(hello) dup length string copy</lang> |
<lang postscript>(hello) dup length string copy</lang> |
||
=={{header|Prolog}}== |
|||
Values in Prolog are immutable so unifying with a variable that already has the value of a string will effectively copy that string. |
|||
You cannot reassign a value once it has been unified, it is not logical to have a value equal more than one thing. |
|||
<lang prolog>?- A = "A test string", A = B. |
|||
A = B, B = "A test string".</lang> |
|||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
Line 1,767: | Line 1,727: | ||
To actually create a copy the <code>Clone()</code> method can be used: |
To actually create a copy the <code>Clone()</code> method can be used: |
||
<lang powershell>$dup = $str.Clone()</lang> |
<lang powershell>$dup = $str.Clone()</lang> |
||
=={{header|PureBasic}}== |
|||
<lang PureBasic>src$ = "Hello" |
|||
dst$ = src$</lang> |
|||
=={{header|ProDOS}}== |
=={{header|ProDOS}}== |
||
Line 1,776: | Line 1,733: | ||
editvar /newvar /value=c /userinput=1 /title=Enter file to copy to: |
editvar /newvar /value=c /userinput=1 /title=Enter file to copy to: |
||
copy -a- from -b- to -c- </lang> |
copy -a- from -b- to -c- </lang> |
||
=={{header|Prolog}}== |
|||
Values in Prolog are immutable so unifying with a variable that already has the value of a string will effectively copy that string. |
|||
You cannot reassign a value once it has been unified, it is not logical to have a value equal more than one thing. |
|||
<lang prolog>?- A = "A test string", A = B. |
|||
A = B, B = "A test string".</lang> |
|||
=={{header|PureBasic}}== |
|||
<lang PureBasic>src$ = "Hello" |
|||
dst$ = src$</lang> |
|||
=={{header|Python}}== |
=={{header|Python}}== |
||
Line 1,825: | Line 1,792: | ||
(printf "~a~a~a~a\n" s1 s2 s3 s4)) ; outputs "HeyHey!!!!!!" |
(printf "~a~a~a~a\n" s1 s2 s3 s4)) ; outputs "HeyHey!!!!!!" |
||
</lang> |
</lang> |
||
=={{header|Raku}}== |
|||
(formerly Perl 6) |
|||
There is no special handling needed to copy a string; just assign it to a new variable: |
|||
<lang perl6>my $original = 'Hello.'; |
|||
my $copy = $original; |
|||
say $copy; # prints "Hello." |
|||
$copy = 'Goodbye.'; |
|||
say $copy; # prints "Goodbye." |
|||
say $original; # prints "Hello."</lang> |
|||
You can also bind a new variable to an existing one so that each refers to, and can modify the same string. |
|||
<lang perl6>my $original = 'Hello.'; |
|||
my $bound := $original; |
|||
say $bound; # prints "Hello." |
|||
$bound = 'Goodbye.'; |
|||
say $bound; # prints "Goodbye." |
|||
say $original; # prints "Goodbye."</lang> |
|||
<!-- SqrtNegInf 2016-01-16 This is NYI, so until such time as it is, leaving this section commented |
|||
You can also create a read-only binding which will allow read access to the string but prevent modification except through the original variable. |
|||
<lang perl6># y $original = 'Hello.'; |
|||
#my $bound-ro ::= $original; |
|||
#say $bound-ro; # prints "Hello." |
|||
#try { |
|||
# $bound-ro = 'Runtime error!'; |
|||
# CATCH { |
|||
# say "$!"; # prints "Cannot modify readonly value" |
|||
# }; |
|||
#}; |
|||
say $bound-ro; # prints "Hello." |
|||
$original = 'Goodbye.'; |
|||
say $bound-ro; # prints "Goodbye."</lang> |
|||
--> |
|||
=={{header|Raven}}== |
=={{header|Raven}}== |
||
Line 1,892: | Line 1,894: | ||
<lang rexx>src = "this is a string" |
<lang rexx>src = "this is a string" |
||
dst = src</lang> |
dst = src</lang> |
||
=={{header|RLaB}}== |
|||
<lang RLaB>>> s1 = "A string" |
|||
A string |
|||
>> s2 = s1 |
|||
A string</lang> |
|||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
Line 1,904: | Line 1,900: | ||
cStr2 = cStr1 # make new string from original |
cStr2 = cStr1 # make new string from original |
||
</lang> |
</lang> |
||
=={{header|RLaB}}== |
|||
<lang RLaB>>> s1 = "A string" |
|||
A string |
|||
>> s2 = s1 |
|||
A string</lang> |
|||
=={{header|Robotic}}== |
=={{header|Robotic}}== |
||
Line 1,995: | Line 1,997: | ||
<lang slate>[ | :s | s == s copy] applyTo: {'hello'}. "returns False"</lang> |
<lang slate>[ | :s | s == s copy] applyTo: {'hello'}. "returns False"</lang> |
||
=={{header|Smalltalk}}== |
=={{header|Smalltalk}}== |
||
Line 2,100: | Line 2,101: | ||
<pre>Hello World! |
<pre>Hello World! |
||
I'm gone</pre> |
I'm gone</pre> |
||
=={{header|Vim Script}}== |
=={{header|Vim Script}}== |
||
<lang vim>let str1 = "original string" |
<lang vim>let str1 = "original string" |
||
Line 2,181: | Line 2,183: | ||
ret |
ret |
||
</lang> |
</lang> |
||
=={{header|XPL0}}== |
=={{header|XPL0}}== |