<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="312553" data-post-id="312553">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="daviaws" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/daviaws/120/30531_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  daviaws
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Complete solution thank to <a class="mention" href="/u/al2o3cr" rel="nofollow">@al2o3cr</a> <a href="https://forum.elixirforum.com/t/need-help-with-ets-match-i-always-end-up-with-an-empty-list/60604/8" rel="nofollow">answer</a></p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule ETS.Consume do
  @moduledoc """
  History by key, month, age, value

    r ETS.Consume

    ETS.Consume.scenario()

    table = ETS.Consume.start()
    ETS.Consume.populate(table)
    ETS.Consume.list(table, "12345678901")
    ETS.Consume.list_period(table, "12345678901", 2, 2021, 10, 2021)
  """
  @table :cons
  @opts [:ordered_set, :protected]
  def start do
    :ets.new(@table, @opts)
  end

  def index(age, month) do
    age * 100 + month
  end

  def insert(ref, key, month, age, bytes) do
    index = index(age, month)
    key = {key, index, age, month}

    :ets.insert(ref, {key, bytes})
  end

  def list(table, key) do
    :ets.match(table, {{key, :_, :"$1", :"$2"}, :"$3"})
  end

  def list_period(table, key, begin_month, begin_age, end_month, end_age) do
    begindex = index(begin_age, begin_month)
    endex = index(end_age, end_month)

    # :ets.fun2ms(
    #   fn {{key, index, month, age}, bytes} = _entry when
    #     key == "12345678901" and
    #     index &gt;= 202301 and index &lt;= 202312 -&gt;
    #         {month, age, bytes}
    # end)
    match_spec = [
      {{{:"$1", :"$2", :"$3", :"$4"}, :"$5"},
       [
         {:andalso, {:andalso, {:==, :"$1", key}, {:&gt;=, :"$2", begindex}},
          {:"=&lt;", :"$2", endex}}
       ], [{{:"$3", :"$4", :"$5"}}]}
    ]


    :ets.select(table, match_spec)
  end

  def populate(table) do
    insert(table,  "12345678901", 1, 2021, 100)
    insert(table, "12345678901", 2, 2021, 200)
    insert(table, "12345678901", 3, 2021, 300)
    insert(table, "12345678901", 4, 2021, 200)
    insert(table, "12345678901", 5, 2021, 300)
    insert(table, "12345678901", 6, 2021, 350)
    insert(table, "12345678901", 7, 2021, 200)
    insert(table, "12345678901", 8, 2021, 100)
    insert(table, "12345678901", 9, 2021, 500)
    insert(table, "12345678901", 10, 2021, 300)
    insert(table, "12345678901", 11, 2021, 200)
    insert(table, "12345678901", 12, 2021, 310)
    insert(table, "12345678901", 1, 2022, 410)
    insert(table, "12345678902", 1, 2021, 310)
  end

  def scenario do
    table = ETS.Consume.start()
    ETS.Consume.populate(table)
    # ETS.Consume.list(table, "12345678901")  |&gt; dbg()
    ETS.Consume.list_period(table, "12345678901", 1, 2021, 9, 2021)
  end
end
</code></pre> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="312553" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/need-help-with-ets-match-i-always-end-up-with-an-empty-list/60604/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-312553" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="312553"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-solved cat-solved" title="Marked as solution"></div>
  </section>
</div>
    <div class="postbit" id="312555" data-post-id="312555">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="ityonemo" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/ityonemo/120/11341_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  ityonemo
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>for elixir, fun2ms only works in the shell.</p>
<p>Also, MatchSpec has “ms2fun”, so you can debug in the other direction if it’s not doing what you expect.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="312555" data-batch-url="/posts/batch_likers">
                        2
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/need-help-with-ets-match-i-always-end-up-with-an-empty-list/60604/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-312555" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="312555"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>