I see the variable a does not require interpolation, why does i require it?

eval evaluates expressions in a module’s global scope. a is defined in the 
global scope, but i is local to the for loop and is undefined outside of 
the loop.

— Mike
​
On Wednesday, 21 October 2015 11:18:34 UTC+2, 2n wrote:
>
> Thank you Mike, your solution works.
> However, I don't understand why the $ interpolation is required. I see the 
> variable *a* does not require interpolation, why does *i* require it?
>
> On Wednesday, 21 October 2015 15:30:42 UTC+8, Michael Hatherly wrote:
>>
>> The try block is hiding an error, ERROR: UndefVarError: i not defined. 
>> You need to interpolate the i variable in the quoted expression using $i:
>>
>> for i=1:4
>>     try
>>         if i==3 error() end
>>         eval(:(push!(a,$i)))
>>     catch e
>>     end
>> end
>>
>> — Mike
>> ​
>> On Wednesday, 21 October 2015 09:11:44 UTC+2, 2n wrote:
>>>
>>> I'm trying to eval an expression in a try block and it doesn't seem to 
>>> work.
>>> a=[]
>>> for i=1:4
>>>     try
>>>         if i==3 error() end
>>>         eval(:(push!(a,i)))
>>>         catch e
>>>     end
>>> end
>>>
>>> a
>>>
>>> After running the above code a evaluates to: 
>>>
>>> 0-element Array{Any,1}
>>>
>>> I'm expecting and want the same result as the code below which evaluates 
>>> to: 
>>> 3-element Array{Any,1}:
>>>  1
>>>  2
>>>  4
>>>
>>> a=[]
>>> for i=1:4
>>> try
>>>  if i==3 error() end
>>>  push!(a,i)
>>>  catch e
>>>  end
>>> end
>>>
>>> a
>>>
>>> Is there a way to get expression evalution to work in this case?
>>>
>>>

Reply via email to