title: Updating an array in a loop in Julia, avoiding global variables
body: <p>According to the performance tips for Julia in <a href="https://docs.julialang.org/en/v1/manual/performance-tips/index.html" rel="nofollow noreferrer">https://docs.julialang.org/en/v1/manual/performance-tips/index.html</a>, we should avoid <code>global</code>variables. How can I then updated arrays in <code>while</code> loops avoiding <code>global</code> variables? As an example, I reproduce a very simple case, but I have more complicated cases of <code>while</code> loops, where I must always update <code>global</code> variables at each iteration:</p>
  
  <pre><code>i = 1 
  x = 10
  
  while i &lt;10
      global x = x+2
      global i = i + 1
  end
  </code></pre>
  
  <p>And here is a more complicated example, based on a code of mine:</p>
  
  <pre><code>function consensus(p::Float64)        
      prop=[1 rand() rand() rand() rand(); rand() 1 rand() rand() rand(); rand() rand()  1 rand() rand(); rand() rand() rand() 1 rand(); rand() rand() rand() rand() 1]
      prop=Symmetric(prop)
      thresh=sort(unique(prop))
      g=map(i -&gt; adjmat(proptotmemecl,thresh,i),1:length(thresh))
      global listeclust=map(i -&gt; comp(g,i),1:length(thresh))
      global sizeclust=map(i -&gt; nbcc(listeclust,i),1:length(thresh))
      global sizesdelete=sort(filter(x-&gt; x&lt;=1000^p,unique(reduce(vcat,sizeclust))))
      while length(sizesdelete)&gt;0
          ind2=map(i -&gt; ind(listeclust,sizesdelete,i),1:length(thresh))
          ind4=map(i-&gt; closest(prop,i),ind2)
          map(i -&gt; agglom(g,ind2,ind4,i),1:length(thresh))
          global listeclust=map(i -&gt; comp(g,i),1:length(thresh))
          global sizeclust=map(i -&gt; nbcc(listeclust,i),1:length(thresh))
          global sizesdelete=sort(filter(x-&gt; x&lt;= 1000^p,unique(reduce(vcat,sizeclust))))
      end
      listeclustuniq=unique(listeclust)
  end
  </code></pre>
  
  <p>Here, I created a function with arrays defined inside, where the <code>while</code> loop updates 3 of the arrays. <code>adjmat</code>, <code>comp</code>, <code>nbcc</code>, <code>ind</code>, <code>closest</code> and <code>agglom</code> are functions created earlier in my code. What I would like to know here is how I can update the 3 arrays <code>listeclust</code>, <code>sizeclust</code> and <code>sizesdelete</code> avoiding the <code>global</code> definition, for speeding performance. </p>