last
.. last(coll)
Get the last element of an ordered collection, if it can be computed in O(1) time.
This is accomplished by calling :func:`endof` to get the last index.
Returns the end point of a :obj:`Range` even if it is empty.
Examples
julia> foo = [1 2 3; 4 5 6]
2x3 Array{Int64,2}:
1 2 3
4 5 6
julia> last(foo)
6
julia> last([-1:-2:-10])
-9
julia> last(1:2:0) # empty range
0
In Julia, the function last(coll)
is used to retrieve the last element of an ordered collection. It returns the end point of a Range
even if it is empty. The last element is obtained by calling the endof
function, which calculates it in O(1) time.
Here are some examples of how the last
function can be used:
-
Get the last element of an array:
julia> arr = [10, 20, 30, 40]; julia> last(arr) 40
This example retrieves the last element of the array
arr
. -
Retrieve the last element of a string:
julia> str = "Julia"; julia> last(str) 'a'
It returns the last character of the string
str
. - Obtain the last element of a range:
julia> rng = 1:5; julia> last(rng) 5
The
last
function can also be used with ranges to get the last element.
Note that the last
function is only applicable to ordered collections. If the collection is empty, it may return an error. Ensure that the collection is not empty before using the last
function.
See Also
Array, broadcast, cat, combinations, conj!, digits!, fieldnames, fill, fill!, last, length, maximum, minimum, ones, parent, parentindexes, partitions, permutations, pointer, pointer_to_array, promote_shape, rand!, reshape, scale, similar, sum, sum_kbn, takebuf_array, transpose!, vec, zeros,User Contributed Notes
Add a Note
The format of note supported is markdown, use triple backtick to start and end a code block.