[erlang-questions] Getting the position of a list item
Igor Ribeiro Sucupira
Thu Dec 3 02:16:48 CET 2009
By the way, this is the function (with some details like module names
field_value(R, F) when is_record(R, test), is_atom(F) ->
P = find_first(F, record_info(fields, test)),
lists:nth(P + 1, tuple_to_list(R)).
I don't think there would be something easier without the function to
get the position of the list item (find_first, in my example).
On Wed, Dec 2, 2009 at 10:58 PM, Igor Ribeiro Sucupira <> wrote:
> Hum... when you talked about records, I realized I still have one
> function that needs to find the position of an item on a list. The
> function is used to get the value of a field F on a record, when F is
> defined at runtime.
> I don't understand exactly what you need, but I guess it's basically
> the same thing, isn't it?
> On Wed, Dec 2, 2009 at 3:53 PM, Garrett Smith <> wrote:
>> On Wed, Dec 2, 2009 at 11:34 AM, Robert Raschke <> wrote:
>>> On Wed, Dec 2, 2009 at 5:10 PM, Garrett Smith <> wrote:
>>>> I'm missing something basic here :\
>>>> The lists module has keyfind/3 but no find/2, where you want to return
>>>> the position of a particular list item. This is surprising.
>>>> Is there a commonly used pattern for this?
>>> Hmm, I fail to come up with a good usage scenario for wanting this. What are
>>> you needing the position of an item in a list for?
>> I'm stitching together record field values and want to get an
>> insertion position using a field token.
>> So, this goes in hard with an "insert_at(Value, N, List_Or_Tuple)"
>> type of function.
>> In general though, there are several functions in the lists module
>> that accept a positional argument (N) -- having some easy ways of
>> getting N seems reasonable. Functions like l/find and rfind are pretty
>> common in the list APIs I'm familiar with.
>> erlang-questions mailing list. See http://www.erlang.org/faq.html
>> erlang-questions (at) erlang.org
> "The secret of joy in work is contained in one word - excellence. To
> know how to do something well is to enjoy it." - Pearl S. Buck.
"The secret of joy in work is contained in one word - excellence. To
know how to do something well is to enjoy it." - Pearl S. Buck.
More information about the erlang-questions