# [erlang-questions] How would you do 100 ifs?

Dmitry Demeshchuk <>
Tue Nov 15 14:58:23 CET 2011

```Using a list isn't good here, use balanced trees (gb_trees won't suite
most likely though). There's an entry at Eugeny Kirpichev's
Livejournal describing how he solved a similar task (it was for IPs
and in Haskell, but that doesn't make much difference).

On Tue, Nov 15, 2011 at 4:43 PM, Torben Hoffmann
<> wrote:
> I would assume that inifinity is 10,000,000 (that is a pretty big number, if
> it is not enough you can just use X * 10,000,000 as infinity) and then I
> would create a dict and store an index for each number between 0 and
> 10,000,000 based on the index in the list and then look up the value for X
> in the dict.
>
> Something like this:
>
> {_,Lookup} = lists:foldl( fun(Ls,{Index,Dict}) ->
>                                         NewDict = lists:foldl( fun(L, D) ->
> dict:store(L,Index,D) end,
>                                                                Dict,
>                                                                Ls),
>                                         {Index+1, NewDict}
>                           end,
>                          {0, dict:new()},
>                          ListOfRanges),
> dict:find(X,Lookup).
>
> But bear with me I have not given this much thought let alone pushed through
> the compiler.
>
> Cheers,
> Torben
>
>
> On 15/11/11 14:27 , Max Bourinov wrote:
>
> Hi guys,
> I have a value X which is integer, and I have a list of ranges
> [0....200),[200....600),[600...1000)....etc....[100000, infinity] (this is
> just an example). We can assume that the list is static.
> I have frequently check the index of the range X belongs to.
> How would you implement it in Erlang?
> Best regards,
> Max
>
> _______________________________________________
> erlang-questions mailing list
>
> http://erlang.org/mailman/listinfo/erlang-questions
>
> _______________________________________________
> erlang-questions mailing list
>
> http://erlang.org/mailman/listinfo/erlang-questions
>
>

--
Best regards,
Dmitry Demeshchuk

```