title: Elixir - modifying values external to an anonymous function
body: <p>First, I am absolutely sure that I'm going about this the wrong way as I'm still learning Elixir coming from Ruby...</p>
  
  <p>I get a list of search results from youtube and am trying to extract the video with the most views.</p>
  
  <pre><code># html is the contents of the search results page
  metas = html |&gt; Floki.find(".yt-lockup-meta-info &gt; li")
  
  counter = -1
  index = -1
  high_views = 0
  
  Enum.each(metas, fn(li) -&gt;
    counter = counter + 1
    text = Floki.text(li)
    case String.split(text, " ") do
      [count, "views"] -&gt;
        views = String.to_integer(String.replace(count, ",", ""))
        IO.puts("&gt;&gt;&gt; #{counter} - #{to_string(views)} views")
        if views &gt; high_views do
          high_views = views
          index = counter
        end
      [age, time_measurement, "ago"] -&gt;
        nil
    end
  end)
  </code></pre>
  
  <p><code>metas</code> is a list of <code>li</code> tuples, like this:</p>
  
  <pre><code>[{"li", [], ["2 years ago"]}, {"li", [], ["5,669,783 views"]},
   {"li", [], ["9 years ago"]}, {"li", [], ["17,136,804 views"]},
   ...
   {"li", [], ["1 year ago"]}, {"li", [], ["15,217 views"]},
   {"li", [], ["8 years ago"]}, {"li", [], ["909,053 views"]}]
  </code></pre>
  
  <p>This won't work because the anonymous function passed to <code>Enum.each</code> has its own scope and doesn't set the values for <code>index</code> and <code>high_views</code>.</p>
  
  <p>Is there a way to pass values from the outside scope into the anonymous function? Or maybe a better question is, how should I go about doing this?</p>
  
  <p>I intended to get it working and then refactor the code but I'm stuck. Thanks for any help.</p>