juliaeval# Modifying an Indexed Value in a Vector Stored as an Expression in Julia

I have a Julia vector ** a** defined as follows:

```
a = [1, 2, 3]
```

I also have an expression ** exp** stored, which represents the indexing of the second element in vector

`a`

```
exp = :(a[2])
```

Now, I would like to modify the value at the indexed position, i.e., set ** a[2]** to a new value, say

`4`

I've attempted the usual assignment, but it doesn't seem to work as expected:

```
$exp = 4 # This results in an error
```

I've also tried using ** eval**, but it doesn't seem to update the vector

`a`

```
eval(exp) = 4 # This doesn't update the vector a
```

Could someone please provide guidance on how to correctly modify the indexed value in the vector ** a** when the index is stored as an expression? Any insights or alternative approaches would be greatly appreciated.

Solution

`eval`

an expression using the first one and make there the update.

```
a = [1, 2, 3]
ex = :(a[2])
eval(:($ex = 4))
a
#3-element Vector{Int64}:
# 1
# 4
# 3
```

- Adding constraints to jump model from dict
- Non-iterable argument to a function called by Julia `map`?
- Compute row sums and column sums efficiently in Julia
- Julia copying folder into an existing folder
- How to force Julia to use multiple threads for matrix multiplication?
- Can Revise.jl handle `ERROR: LoadError: invalid redefinition of constant`?
- Define piecewise function with automatic broadcasting in Julia
- julia Handling time difference in dataframe
- Why do allocations occur during broadcasting assignment to a preallocated array?
- Comparing RK4 to Euler method
- How to put even numbers from matrix in separate vector in Julia?
- Number of iterations performed by a for-loop in Julia
- Can I redefine a function, but still use the old definition within the new definition?
- how do I use analytical form as gradient with ! function?
- Julia equivalent to R `as.numeric()`
- Why does Float16(1.1)-Float16(1)=Float16(0.0996)?
- Julia manual and defining an infix operator
- In Julia, how to convert a unsigned number to a signed number like in C?
- Julia Package DataFrames 1.6.1 doesn't recognize old version DataFrames 1.3.4 file
- Convert Julia DataFrame to an array of bytes for compression
- How to suppress svg output by IJulia in Jupyter notebooks
- how to get the documentation of a julia function in a vscode notebook?
- Passing command-line arguments to a Pluto Notebook
- Julia gives "MethodError: rand!" when trying to optimize over a certain manifold using Manopt
- Why doesn't the loss calculated by Flux `withgradient` match what I have calculated?
- How to import Julia packages into Python
- Obtain the number of CPU cores in Julia
- Absolute path of the project root directory in Julia
- Analog of r-here or py-here for Julia
- Concatenating arrays in Julia