title: Looping a map and updating key values in Elixir
body: <pre><code>def sort(someString) do
  
      countSubstring = fn(_, "") -&gt; 0
               (str, sub) -&gt; length(String.split(str, sub)) - 1 end
  
      someMap = %{:a =&gt; 0, :b =&gt; 0, :c =&gt; 0, :d =&gt; 0}
  
      def loopMap(aMap, n) when n &lt;= 1 do
          timesKeyFound = countSubstring(someString, aMap[n])
          Map.put_new(aMap, aMap[key], timesKeyFound)
      end
  
      def loopMap(aMap, n) do
          timesKeyFound = countSubstring(someString, aMap[n])
          Map.put_new(aMap, aMap[key], timesKeyFound)
          loopMap(aMap, n - 1)
      end
  
      loopMap(someMap, 4) #4 is the number of keys in the map
  end
  </code></pre>
  
  <p>What I am trying to do here is to pass a string into the sort module. For example let's pass the string "Hi my name is bob". Once this is passed I create a map with 4 keys: 'a' 'b' 'c' and 'd'. 
  Then in my loopMap code I am trying to pass my someMap map and the number of its keys (n) as arguments. Then I search the string for instances of each key and if the key is found I update my map by 1 for each key found.</p>
  
  <p>after I run the loopMap on my someMap i should have an updated map which is:</p>
  
  <pre><code>%{a: 1, b: 2, c: 0, d: 0}
  </code></pre>
  
  <p>What am i doing wrong? </p>