Hey guys,
Total noob, just so ya know ;)
The 'Slices' section in the Tutorial has me a bit confused. There's this example that uses the ^ operator:
b[11..^2] = "useful"
I've been able to figure out what this does by experimenting, but it isn't explained at all in this section. The only other mention of something similar to this is in the earlier section on 'For' but there isn't actually an example of its use.
Could really use some help in understanding this, and also any links to where in the docs I can learn more. It's a difficult thing to "google" for.
Thanks!
I'll give it a try:
b[0..^1] is equivalent to b[0..b.len-1]
where ^1 is a "shortcut" that makes it easy to get the last element.
Because the string ends in a period (b = "Slices are useless.") then to get the portion of the string that is "useless" and replace it with "useful", remembering that indices are zero based,
"Slices are useless."
| | |
0 11 17 using indices
^19 ^8 ^2 using ^ syntax
then
b[11..^2] is the portion "useless" and
b[11..^2] = "useful" replaces the "useless" portion with "useful"
and hence
echo b displays Slices are useful.
Note: you could also do it as b[^8..^2] = "useful" or as b[11..b.len-2] = "useful" or as b[11..<b.len-1] = "useful" or as b[^8..^2] = "useful" or as ....
Will do.
Feature request: Can the tutorials get the same Edit link that the docs do, for easy PR generation ?