help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Accessing an element in an associativity list


From: Joost Kremers
Subject: Re: Accessing an element in an associativity list
Date: 9 Sep 2008 13:28:34 GMT
User-agent: slrn/0.9.8.1 (Linux)

Nordlöw wrote:
> I have the following list of lists structure
>
> (defvar c++-stl-containers
>   '(
>     ("string" "<string>" "String of char.")
>     ("wstring" "<wstring>" "String of wide char (wchar_t).")
>     ("vector" "<vector>" "Vectors contain contiguous elements stored
> as an array.")
>    )
>  )

note, lisp is not C, so don't depend on the parens to show grouping, use
indentation for visual cues. let emacs handle the parentheses. just format
the code like so:

(defvar c++-stl-containers
  '(("string" "<string>" "String of char.")
    ("wstring" "<wstring>" "String of wide char (wchar_t).")
    ("vector" "<vector>" "Vectors contain contiguous elements stored as an 
array.")))

> and I want to do something like this:
>   (index-first 'c++-stl-containers "string" 2)
> should evaluate to
>   "<string>"
> 
> Is there any emacs lisp convenience function for accessing a row given
> its, say, first (key) element?

you can use ASSOC:

(assoc "string" c++-stl-containers)

  ===> ("string" "<string>" "String of char.")

ASSOC uses EQUAL as comparison predicate, so you can use it with strings as
keys.

(info "(elisp) Association Lists") for more details.

> I know this is slow but speed is of no
> importance here. Of course if a hash table is as easy to use then of
> course I would like to use that.

(info "(elisp) Hash Tables") should tell you all you need to know. note
that hash tables by default uses eql for key lookup. if you want to use
strings as keys, pass a :test equal argument.

HTH


-- 
Joost Kremers                                      joostkremers@yahoo.com
Selbst in die Unterwelt dringt durch Spalten Licht
EN:SiS(9)


reply via email to

[Prev in Thread] Current Thread [Next in Thread]