[erlang-questions] index for lists and tuples?

Sam Bobroff samb@REDACTED
Fri Apr 17 08:27:18 CEST 2009


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Thu, 16 Apr 2009 22:16:31 -0700
Steve Kirsch <steve.kirsch@REDACTED> wrote:

> I am looking for an index function for lists and tuples that returns
> the index of the first element that matches a query term.
> 
> For example:
> 
> index({a, b, c}, b) would return 2
> 
> index([a, b, c], c) would return 3

[snip]

This isn't a general solution, but if you're searching in a list of
characters (e.g. integers), you can use string:chr to do what you want.

1> string:chr([4, 5, 6], 5).
2

I suspect if you're going to do lookups like that, a better solution
would be to use a key/value list (see the lists module) or an orddict
(see the orddict module).

Cheers,
Sam.
- -- 
Sam Bobroff | sam@REDACTED | M5 Networks
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEARECAAYFAknoIUYACgkQm97/UHSa/ARl8gCfTjDi3swqN4S9S/uJJjphxqJk
cEQAnjPLufYEVPRj4extyL4S9OiTjFYl
=bn8d
-----END PGP SIGNATURE-----


More information about the erlang-questions mailing list