# Using subscript to index an array

**URL:** <https://discourse.julialang.org/t/using-subscript-to-index-an-array/115190>\
**Category:** New to Julia\
**Tags:** indexing, unicode\
**Created:** [June 4, 2024, 5:34pm UTC](https://discourse.julialang.org/t/using-subscript-to-index-an-array/115190 "2024-06-04T17:34:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bsuwal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bsuwal/32/19577_2.png) [@bsuwal](https://discourse.julialang.org/u/bsuwal)\
**Post date:** [June 4, 2024, 5:34pm UTC](https://discourse.julialang.org/t/using-subscript-to-index-an-array/115190/1 "2024-06-04T17:34:49Z")

</div>

Is there any macro or functionality that would allow me to index the elements of an array by subscript?

For eg if I had an array:  
`A = [1, 2, 3]`  
I would like to have code such as:

```julia
for i=1:length(A)
    A\_i = A\_i * 3
end

```

that would, in this example, multiply each element of `A` by 3.

My goals are for purely aesthetic/readability reasons. I think such a macro, if it exists, would be especially nice to index tensors or matrices. Thank you!

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [June 4, 2024, 5:38pm UTC](https://discourse.julialang.org/t/using-subscript-to-index-an-array/115190/2 "2024-06-04T17:38:27Z")

</div>

> [@bsuwal](#):
>
> Is there any macro or functionality that would allow me to index the elements of an array by subscript?

You could certainly write a macro that transforms variable names like `Aᵢ` or `A_i` (but not `A\_i`, which won’t parse as a variable name) into expressions `A[i]`. I don’t know of any existing code for this, off the top of my head.

I would tend to discourage you from doing this, however, except perhaps as a learning exercise — the resulting code will not be very readable to anyone else. It is generally better to write idiomatic Julia code.

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [June 4, 2024, 6:03pm UTC](https://discourse.julialang.org/t/using-subscript-to-index-an-array/115190/3 "2024-06-04T18:03:55Z")

</div>

> [@bsuwal](#):
>
> `A\_i = A\_i * 3`

For a while TensorCast.jl had a variant macro which understood `A_i` instead of `A[i]`, although not `Aᵢ`. But I removed it in the name of simplicity. Right now:

```julia
julia> using TensorCast

julia> @cast A[i] = A[i] * 3 # equivalent to the loop above
3-element Vector{Int64}:
 3
 6
 9

julia> cast"A_i = A_i * 3" # no longer exists
ERROR: LoadError: UndefVarError: `@cast_str` not defined

```

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [June 4, 2024, 7:20pm UTC](https://discourse.julialang.org/t/using-subscript-to-index-an-array/115190/4 "2024-06-04T19:20:58Z")

</div>

I’ll sometimes write code like:

```julia
for (i, Aᵢ) in enumerate(A)
    A[i] = Aᵢ * 3
end

```

Of course, re-assigning `Aᵢ` doesn’t affect `A` itself so you still need to use indexed assignment there, but in some situations it can result in nicely-readable for loops.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [June 4, 2024, 7:34pm UTC](https://discourse.julialang.org/t/using-subscript-to-index-an-array/115190/5 "2024-06-04T19:34:05Z")

</div>

> [@mbauman](#):
>
> `for (i, Aᵢ) in enumerate(A)`

Note that `for (i, Aᵢ) in pairs(A)` is slightly more general, here, since `pairs` doesn’t assume 1-based indexing.
