unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Accessing an element in an associativity list
@ 2008-09-09 13:07 Nordlöw
  2008-09-09 13:28 ` Joost Kremers
  0 siblings, 1 reply; 3+ messages in thread
From: Nordlöw @ 2008-09-09 13:07 UTC (permalink / raw)
  To: help-gnu-emacs

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.")
   )
 )

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? 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.

Thanks in advance,
Nordlöw


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Accessing an element in an associativity list
  2008-09-09 13:07 Accessing an element in an associativity list Nordlöw
@ 2008-09-09 13:28 ` Joost Kremers
  2008-09-09 14:47   ` Tassilo Horn
  0 siblings, 1 reply; 3+ messages in thread
From: Joost Kremers @ 2008-09-09 13:28 UTC (permalink / raw)
  To: help-gnu-emacs

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)


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Accessing an element in an associativity list
  2008-09-09 13:28 ` Joost Kremers
@ 2008-09-09 14:47   ` Tassilo Horn
  0 siblings, 0 replies; 3+ messages in thread
From: Tassilo Horn @ 2008-09-09 14:47 UTC (permalink / raw)
  To: help-gnu-emacs

Joost Kremers <joostkremers@yahoo.com> writes:

Hi!

> (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.")

For the sake of completeness:  To get to "<string>" from the key
"string" you'd use

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

here, which is a shortcut for

      (car (cdr (assoc "string" c++-stl-containers))).

Or you could use one of

      (nth 1 (assoc "string" c++-stl-containers))
      (second (assoc "string" c++-stl-containers)).

Bye,
Tassilo





^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2008-09-09 14:47 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-09-09 13:07 Accessing an element in an associativity list Nordlöw
2008-09-09 13:28 ` Joost Kremers
2008-09-09 14:47   ` Tassilo Horn

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).