# How to change or redefine a (global?) variable from within a while loop

**URL:** https://discourse.julialang.org/t/how-to-change-or-redefine-a-global-variable-from-within-a-while-loop/21086
**Category:** New to Julia
**Created:** [February 22, 2019, 4:50pm UTC](https://discourse.julialang.org/t/how-to-change-or-redefine-a-global-variable-from-within-a-while-loop/21086 "2019-02-22T16:50:59Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![HelgavonLichtenstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgavonlichtenstein/32/5005_2.png) [@HelgavonLichtenstein](https://discourse.julialang.org/u/HelgavonLichtenstein)
#### Post date: [February 22, 2019, 4:51pm UTC](https://discourse.julialang.org/t/how-to-change-or-redefine-a-global-variable-from-within-a-while-loop/21086/1 "2019-02-22T16:51:00Z")

</div>

I am a beginner Julia user and perhaps I am overlooking something obvious but, how can I change a variable in the global scope from within a while loop?

```julia
using Distributions
#Constants
const Ropt = 100
const γ= 2
const NumSpecies = 50
const m = sort(rand(Uniform(10^2, 10^12), NumSpecies))

L = @. (m/(m'*Ropt) * exp(1-m/(m'*Ropt)))^γ 
L[L .<= 0.01] .= 0
L
#should L be a function? Would it look something like:
# function L(m,Ropt,γ)
# @. (m/(m'*Ropt) * exp(1-m/(m'*Ropt)))^γ
#L[L .<= 0.01] .= 0
#end

FoodWeb = zero(L)
FoodWeb[L .> rand.(Uniform(0, 1))] .= 1
FoodWeb

while (sum(FoodWeb, dims=(1))== 0) & (sum(FoodWeb, dims =(2))==0) #if both a column and a row has all zeros
  FoodWeb = zero(L) #I want to redo FoodWeb
  FoodWeb[L .> rand.(Uniform(0, 1))] .= 1
  return FoodWeb #here I want to stop remaking FoodWeb and place the new values of it into the previous global scope
end

```

I know that tuples are immutable and perhaps FoodWeb is a tuple?

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [February 22, 2019, 4:56pm UTC](https://discourse.julialang.org/t/how-to-change-or-redefine-a-global-variable-from-within-a-while-loop/21086/2 "2019-02-22T16:56:32Z")

</div>

Use the `global` keyword to assign to a global from a loop.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [February 22, 2019, 5:39pm UTC](https://discourse.julialang.org/t/how-to-change-or-redefine-a-global-variable-from-within-a-while-loop/21086/3 "2019-02-22T17:39:54Z")

</div>

> [@HelgavonLichtenstein](#):
>
> ```julia
> # here I want to stop remaking FoodWeb and place the new values of it into the previous global scope
> 
> ```

I think you are confusing changing the _value_ of a global variable with changing the _contents_ of a container. The latter operation does not change the container, only the contents.

If the above is related to [this](https://discourse.julialang.org/t/difference-between-global-and-const/21075) and [this](https://discourse.julialang.org/t/difference-between-global-and-const/21075) question, you may be better off writing a function that just modifies an array, eg

```julia
function update_foodweb!(food_web, other, arguments)
    if something
        food_web[some_coordinates, :] = updated_stuff
    end
    food_web
end

```

---

<div class="post-metadata">

### Author: ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)
#### Post date: [February 22, 2019, 8:39pm UTC](https://discourse.julialang.org/t/how-to-change-or-redefine-a-global-variable-from-within-a-while-loop/21086/4 "2019-02-22T20:39:57Z")

</div>

> [@HelgavonLichtenstein](#):
>
> sum(FoodWeb, dims=(1))== 0

May be this condition is not exactly what you mean? This expression `sum(FoodWeb, dims=(1))` evaluates to a `1×50 Array{Float64,2}` and you are comparing with a scalar `0`, the result is always `false` regardless. Perhaps you mean no whole column and row add up to zero? That’s, both vectors from `sum(..,dims=1)` and `sum(..,dims=2)` can have at most one zero? This is not guaranteed to happen!
