.\" Made by website/plain/build.mjs from website/words/index-from-end.md; change that file. .TH SG-INDEX-FROM-END 7 2026-11-28 "SG" "SG reference" .SH NAME xs[^1] \- an index counted from the end: xs[^1] is the last item, xs[^n] is xs[xs.length() - n] .SH SYNOPSIS .TP \fBxs[^1], xs[^2]\fR the last item, the one before it .TP \fBxs[^n], xs[^(k + 1)]\fR the item n from the end: xs[xs.length() - n] .TP \fBxs[^1] = v, xs[^1] += v, ref xs[^1]\fR written and lent as xs[i] is .TP \fBxs[..^1], xs[1..^1], xs[^3..]\fR an end of a range counted from the end .TP \fBxs[a..^0]\fR at an end of a range, ^0 is the length .TP \fBtext[^1]\fR the last byte of a text .SH DESCRIPTION \fBxs[^n]\fR is \fBxs[xs.length() \- n]\fR: \fB^1\fR is the last item, \fB^2\fR the one before it, and \fB^n\fR on \fBn\fR items the first. It is written on a list, an array, a slice and a text, where it counts the bytes. What is indexed is named once and computed once, so \fBnext()[^1]\fR calls \fBnext\fR once, and the place costs what \fBlength() \- n\fR costs. .PP \fB^\fR also ends a range: \fBxs[..^1]\fR is every item but the last, \fBxs[1..^1]\fR leaves out the first and the last, and \fBxs[^3..]\fR is the last three. At an end of a range \fB^0\fR is the length itself, and valid; as an index it is past the last item, and an error. .PP \fB^\fR is written only first in an index or in an end of a range. It is not a value of its own: \fBvar i = ^1\fR and \fBxs.get(^1)\fR are errors; between two values, \fBa ^ b\fR is the exclusive or of their bits. What follows it is one operand, as after \fB\-\fR: a sum goes in parentheses, \fBxs[^(k + 1)]\fR\&. The count is any integer that widens to \fBnint\fR\&. .PP A count that is a constant is checked when the program is compiled: \fB^0\fR as an index, a negative number after \fB^\fR, and \fB^4\fR on an \fBint[3]\fR are errors, and so is Python's \fBxs[\-1]\fR, with the hint \fB^1\fR\&. A count that is known only when the program runs is checked then, in every build, and a count outside the items stops the program with what was written, \fBindex ^4 is out of range\fR, and a hint that says what may be counted. A \fBHashMap\fR has no end to count from, since its keys are not places in a row. There is no \fBlast()\fR: \fBxs.get(xs.length() \- 1)\fR reads the last item, or \fBnull\fR for an empty list. .SH EXAMPLES .SS "The last items" .RS 4 .nf void main() { var days = List { "mon", "tue", "wed", "thu", "fri" } print(days[^1]) print(days[^2]) print(days[^5]) var primes = int[4] { 2, 3, 5, 7 } print(primes[^1]) } .fi .RE .PP .RS 4 .nf fri thu mon 7 .fi .RE .PP \fBdays[^1]\fR is \fBdays[days.length() \- 1]\fR, with \fBdays\fR written once. On five items \fB^5\fR is the first. .SS "The ends of a range" .RS 4 .nf void main() { var xs = List { 10, 20, 30, 40, 50 } print(xs[..^1]) print(xs[1..^1]) print(xs[^3..]) print(xs[^3..^1]) print(xs[2..^0]) print(xs[^1..^1]) } .fi .RE .PP .RS 4 .nf [10, 20, 30, 40] [20, 30, 40] [30, 40, 50] [30, 40] [30, 40, 50] [] .fi .RE .PP \fBxs[^3..^1]\fR is from the third item from the end up to the last, without the last. \fB^0\fR is the length, so \fBxs[2..^0]\fR is \fBxs[2..]\fR\&. .SS "Writing at the end" .RS 4 .nf void main() { var scores = List { 7, 3, 9, 1, 5 } scores[^1] = 6 scores[^2] += 10 print(scores) scores[^3..].sort() print(scores) var tail = ref scores[^2..] tail[0] = 0 print(scores) } .fi .RE .PP .RS 4 .nf [7, 3, 9, 11, 6] [7, 3, 6, 9, 11] [7, 3, 6, 0, 11] .fi .RE .PP An item counted from the end is written, added to and lent as any item. \fBscores[^3..].sort()\fR puts only the last three in order, and \fBref scores[^2..]\fR lends the last two to write. .SS "A text" .RS 4 .nf void main() { var line = "[42]" print(line[1..^1]) print(line[^1]) print((char)line[^1]) var word = "año" print(word.length()) print(word[..^1]) print(word[^3]) } .fi .RE .PP .RS 4 .nf 42 93 ] 4 añ 195 .fi .RE .PP A text is counted in bytes, as \fBtext[i]\fR is: \fBline[^1]\fR is the byte of \fB]\fR, 93. \fBaño\fR is four bytes, since \fBñ\fR takes two, and \fBword[^3]\fR is the first byte of \fBñ\fR\&. A range from the end of a text gives a \fBread string\fR\&. .SS "A count that is computed" .RS 4 .nf List readings() { print("reading") return List { 4, 8, 15, 16, 23, 42 } } void main() { var xs = List { 10, 20, 30, 40 } for k in 1..xs.length() + 1 { print($"^{k} is {xs[^k]}") } var skip = 1 print(xs[^(skip + 1)]) byte back = 3 print(xs[^back]) print(readings()[^1]) } .fi .RE .PP .RS 4 .nf ^1 is 40 ^2 is 30 ^3 is 20 ^4 is 10 30 20 reading 42 .fi .RE .PP The count may be any integer that widens to \fBnint\fR, such as a \fBbyte\fR\&. A sum after \fB^\fR is put in parentheses. \fBreadings()\fR is called once: what is indexed is kept, and its length found from it. .SS "Lists, slices and consts" .RS 4 .nf const int[4] Primes = int[4] { 2, 3, 5, 7 } const int Largest = Primes[^1] int lastOf(read int[] values) { return values[^1] } void main() { print(Largest) var grid = List> { List { 1, 2 }, List { 3, 4 } } grid[^1][^1] = 9 print(grid) var xs = List { 5, 6, 7, 8 } var front = xs[..2] print(lastOf(front)) print(lastOf(read xs)) } .fi .RE .PP .RS 4 .nf 7 [[1, 2], [3, 9]] 6 8 .fi .RE .PP A slice counts from its own end: the last item of \fBfront\fR is 6, though \fBxs\fR goes on. A const is indexed from the end as from the start. \fBgrid[^1][^1]\fR is the last item of the last list. .SH "WHAT STOPS THE PROGRAM" .SS "A count past the first item" .RS 4 .nf void main() { var xs = List { 10, 20, 30 } var back = 4 print(xs[^back]) } .fi .RE .PP .RS 4 .nf panic: index ^4 is out of range: the length is 3 --> example.sg:4:11 4 | print(xs[^back]) | ^^^^^^^^^ hint: from the end, `^1` is the last and `^3` the first .fi .RE .PP The message names the place as it was written, \fB^4\fR, not \fB\-1\fR, and the hint what may be counted. .SS "The last item of an empty list" .RS 4 .nf void main() { var xs = List() print(xs[^1]) } .fi .RE .PP .RS 4 .nf panic: index ^1 is out of range: the length is 0 --> example.sg:3:11 3 | print(xs[^1]) | ^^^^^^ hint: it is empty, so there is nothing to count from the end .fi .RE .PP \fBxs.get(xs.length() \- 1) ?? ...\fR reads the last item where the list may be empty. For a count that may be 0 or negative it is a panic too: \fBindex ^0 is out of range\fR\&. .SS "An end of a range outside" .RS 4 .nf void main() { var xs = List { 10, 20, 30 } var keep = 4 print(xs[^keep..]) } .fi .RE .PP .RS 4 .nf panic: range bound ^4 is out of range: the length is 3 --> example.sg:4:11 4 | print(xs[^keep..]) | ^^^^^^^^^^^ hint: an end of a range goes from `^0`, the end, to `^3`, the start .fi .RE .PP At an end of a range a count from 0 to the length is valid. .SS "The first and the last of one item" .RS 4 .nf void swapInts(ref int a, ref int b) { var kept = a a = b b = kept } void main() { var xs = List { 1, 2, 3 } swapInts(ref xs[0], ref xs[^1]) print(xs) var one = List { 7 } swapInts(ref one[0], ref one[^1]) } .fi .RE .PP .RS 4 .nf [3, 2, 1] panic: `one[^1]` shares item 0 with `one[0]`, lent to this call too .fi .RE .PP On one item, \fB^1\fR is \fB0\fR, and two \fBref\fR borrows of one item are not allowed at once. .SH "WHAT THE COMPILER REFUSES" .SS "^0 as an index" .RS 4 .nf void main() { var xs = List { 10, 20, 30 } print(xs[^0]) } .fi .RE .PP .RS 4 .nf error: `^0` is the end itself, past the last item, so this index is always out of range --> example.sg:3:14 3 | print(xs[^0]) | ^^ hint: the last item is `^1`; `^0` is written as the end of a range: `[a..^0]` .fi .RE .SS "A negative count" .RS 4 .nf void main() { var xs = List { 10, 20, 30 } print(xs[^-1]) } .fi .RE .PP .RS 4 .nf error: a count from the end is not negative: `^1` is the last item --> example.sg:3:14 3 | print(xs[^-1]) | ^^^ hint: an index from the start is written without `^` .fi .RE .SS "A sum after ^" .RS 4 .nf void main() { var xs = List { 10, 20, 30 } var k = 1 print(xs[^k + 1]) } .fi .RE .PP .RS 4 .nf error: `^` counts one operand from the end; what it counts is put in parentheses --> example.sg:4:14 4 | print(xs[^k + 1]) | ^^^^^^ hint: `^(k + 1)` counts `k + 1` from the end; 1 after `^k` is `^(k - 1)` .fi .RE .PP \fB^k + 1\fR could be read as \fBlength \- (k + 1)\fR or as \fB(length \- k) + 1\fR, so neither is guessed: the hint gives both. Where one reading gives no item, as \fB^(1 \- 1)\fR for \fBxs[^1 \- 1]\fR, the hint gives the other alone: \fB^2\fR\&. .SS "A negative index" .RS 4 .nf void main() { var xs = List { 10, 20, 30 } print(xs[-1]) } .fi .RE .PP .RS 4 .nf error: an index cannot be negative --> example.sg:3:14 3 | print(xs[-1]) | ^^ hint: count from the end with `^`: `^1` .fi .RE .PP Python's \fBxs[\-1]\fR is \fBxs[^1]\fR here. A negative number written as an index or an end of a range of a list, an array, a slice or a text is refused; one known only when the program runs stops it. .SS "^ outside the brackets" .RS 4 .nf void main() { var xs = List { 10, 20, 30 } print(xs.get(^1) ?? 0) } .fi .RE .PP .RS 4 .nf error: `^` counts from the end of what is indexed, so it is written first between `[ ]`: `xs[^1]`, `xs[..^1]` an index from the end is not a value of its own; the bit inverse is `~x` --> example.sg:3:18 3 | print(xs.get(^1) ?? 0) | ^ .fi .RE .PP \fBvar i = ^1\fR is refused the same way. \fBxs.get(xs.length() \- 1)\fR gives the last item or \fBnull\fR\&. .SS "Past the start of an array" .RS 4 .nf void main() { var a = int[3] { 1, 2, 3 } print(a[^4]) } .fi .RE .PP .RS 4 .nf error: index ^4 is out of range: the length is 3 the items of an `int[3]` are at ^1 to ^3 from the end --> example.sg:3:11 3 | print(a[^4]) | ^^^^^ .fi .RE .PP An array's length is part of its type, so a constant count is checked when the program is compiled. .SS "A map" .RS 4 .nf void main() { var ages = HashMap { {1, 36} } print(ages[^1]) } .fi .RE .PP .RS 4 .nf error: a map has no end to count from: its keys are not places in a row --> example.sg:3:16 3 | print(ages[^1]) | ^^ hint: `^` counts from the end of a list, an array, a slice or a text; a key is written as it is: `map[1]` .fi .RE .SH "SEE ALSO" .BR sg-range (7), .BR sg-slice (7), .BR sg-list (7), .BR sg-string (7), .BR sg-nullable (7) .PP https://sg\-lang.org/reference/index-from-end.html