First, I am absolutely sure that I'm going about this the wrong way as I'm still learning Elixir coming from Ruby...
I get a list of search results from youtube and am trying to extract the video with the most views.
# html is the contents of the search results page
metas = html |> Floki.find(".yt-lockup-meta-info > li")
counter = -1
index = -1
high_views = 0
Enum.each(metas, fn(li) ->
counter = counter + 1
text = Floki.text(li)
case String.split(text, " ") do
[count, "views"] ->
views = String.to_integer(String.replace(count, ",", ""))
IO.puts(">>> #{counter} - #{to_string(views)} views")
if views > high_views do
high_views = views
index = counter
end
[age, time_measurement, "ago"] ->
nil
end
end)
metas is a list of li tuples, like this:
[{"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"]}]
This won't work because the anonymous function passed to Enum.each has its own scope and doesn't set the values for index and high_views.
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?
I intended to get it working and then refactor the code but I'm stuck. Thanks for any help.