..  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.


julia> foo = [1 2 3; 4 5 6]
2x3 Array{Int64,2}:
 1  2  3
 4  5  6

julia> last(foo)

julia> last([-1:-2:-10])

julia> last(1:2:0)  # empty range

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:

  1. Get the last element of an array:

    julia> arr = [10, 20, 30, 40];
    julia> last(arr)

    This example retrieves the last element of the array arr.

  2. Retrieve the last element of a string:

    julia> str = "Julia";
    julia> last(str)

    It returns the last character of the string str.

  3. Obtain the last element of a range:
    julia> rng = 1:5;
    julia> last(rng)

    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.

*Required Field

Checking you are not a robot: