reverseind

reverseind(v, i)

Given an index i in reverse(v), return the corresponding index in v so that v[reverseind(v,i)] == reverse(v)[i]. (This can be nontrivial in the case where v is a Unicode string.)

Examples

  1. Get the corresponding index in the original array:

    julia> v = [10, 20, 30, 40];
    julia> i = 2;
    julia> reverseind(v, i)
    3

    This example returns the index in v that corresponds to the element at index i in reverse(v).

  2. Handle Unicode strings:

    julia> str = "𝐇𝐞𝐥𝐥𝐨";
    julia> i = 2;
    julia> reverseind(str, i)
    4

    The reverseind function correctly handles the case of Unicode strings, returning the corresponding index in the original string.

  3. Use with arrays of characters:
    julia> chars = ['J', 'u', 'l', 'i', 'a'];
    julia> i = 4;
    julia> reverseind(chars, i)
    2

    It can be used with arrays of characters, providing the index in the original array.

Common mistake example:

julia> v = [1, 2, 3, 4];
julia> i = 5;
julia> reverseind(v, i)
ERROR: BoundsError: attempt to access 4-element Array{Int64,1} at index [5]

In this example, the index provided is out of bounds for the array. It's important to ensure that the index is within the valid range of the collection to avoid such errors. Always check that the index is a valid position in the collection before using reverseind.

See Also

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
Details

Checking you are not a robot: