# Problem with object update and variable scope inside for loop

**URL:** https://discourse.julialang.org/t/problem-with-object-update-and-variable-scope-inside-for-loop/2173
**Category:** New to Julia
**Created:** [February 18, 2017, 3:27pm UTC](https://discourse.julialang.org/t/problem-with-object-update-and-variable-scope-inside-for-loop/2173 "2017-02-18T15:27:59Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![josimar](https://avatars.discourse-cdn.com/v4/letter/j/cab0a1/32.png) [@josimar](https://discourse.julialang.org/u/josimar)
#### Post date: [February 18, 2017, 3:27pm UTC](https://discourse.julialang.org/t/problem-with-object-update-and-variable-scope-inside-for-loop/2173/1 "2017-02-18T15:27:59Z")

</div>

Hi,

I am a new Julia user, and in programming in general, and I would like some help to understand why updates in the array “pressure” in the code below does not carry over after the for loop.

Note that a similar code in python seems to be working fine. Please let me know what I am missing and, perhaps, a way to get it right.

Thank you in advance for you help and time,

```julia
function Create_Fault_List(s, fault_list_size)
  pressure=zeros(1,s)

  fault=[] 
  for i=1:fault_list_size
    push!(fault,Create_Pressure_Type(pressure))
  end #close fo loop

  return fault

end

type Create_Pressure_Type
  pressure::Array{Float64}  
end

#List with 5 elements
s=5 #size of the pressure vector
fault_list_size = 10 # size of the fault list
fault=Create_Fault_List(s, fault_list_size)

index=3 #index to update in the vector pressure
for k=1:length(fault)
  #I would like to update the index element of the array pressure
  fault[k].pressure[index]=k

  #It seems to work, as you can see here
  println("pressure vector inside the loop ",fault[k].pressure)
end

println("\n")

#However here the pressure array has only the last element
for i=1:length(fault)
  println("pressure vector outside the loop=", fault[i].pressure)
end

```

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [February 18, 2017, 4:01pm UTC](https://discourse.julialang.org/t/problem-with-object-update-and-variable-scope-inside-for-loop/2173/2 "2017-02-18T16:01:49Z")

</div>

You put the same array into every element…

```julia
push!(fault,Create_Pressure_Type(copy(pressure)))

```

if you want to use different arrays all with the same value.

---

<div class="post-metadata">

### Author: ![josimar](https://avatars.discourse-cdn.com/v4/letter/j/cab0a1/32.png) [@josimar](https://discourse.julialang.org/u/josimar)
#### Post date: [March 5, 2017, 10:28pm UTC](https://discourse.julialang.org/t/problem-with-object-update-and-variable-scope-inside-for-loop/2173/3 "2017-03-05T22:28:01Z")

</div>

thank you for reply. It worked fine now !
