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

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

Re: How to convert a char to a property


From: Cecil Westerhof
Subject: Re: How to convert a char to a property
Date: Wed, 08 Dec 2010 15:21:24 -0000
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux)

Op dinsdag 27 jul 2010 19:59 CEST schreef Pascal J. Bourguignon:

>>> I have the following property list:
>>> (defvar dcbl-bbdb-user-defined-fields (list :L "mail-alias"
>>> :M "mailer"
>>> :P "playlist"
>>> :s "subjects"
>>> :t "type"
>>> :w "website")
>>> "User defined fields in the bbdb with parameter to insert into buffer")
>>>
>>> With (getf dcbl-bbdb-user-defined-fields :M) I get "mailer".
>>>
>>> But I want to work parameter driven. So for example I have:
>>> (setq parameters "MLst")
>>> (setq current-char (string-to-char parameters))
>>>
>>> The variable current-char now contains 77. How could I use
>>> current-char to retrieve "mailer"?
>>
>> At the moment I use another solution.
>>
>> I now have:
>> (defvar dcbl-bbdb-user-defined-fields '(("L" "mail-alias")
>> ("M" "mailer")
>> ("P" "playlist")
>> ("s" "subjects")
>> ("t" "type")
>> ("w" "website"))
>> "User defined fields in the bbdb with parameter to insert into buffer")
>>
>> and use the following code:
>> (setq field-name     nil
>> parameters     dcbl-bbdb-user-defined-fields
>> this-value     nil)
>> (while parameters
>> (setq this-parameter (car parameters)
>> parameters     (cdr parameters))
>> (when (string= current-char (car this-parameter))
>> (setq field-name (car (cdr this-parameter))
>> parameters nil)))
>>
>> The variable current-char is the parameter (as a string) that I am
>> searching for.
>>
>> This does work, but I do not know if it is very efficient. Is this a
>> good solution, or should I code it differently?
>
> If you have characters and want to map them, why do you make a table
> mapping keywords or strings?  Make a table mapping characters!
>
>
> (defvar dcbl-bbdb-user-defined-fields '((?L . "mail-alias")
> (?M . "mailer")
> (?P . "playlist")
> (?s . "subjects")
> (?t . "type")
> (?w . "website"))
> "An a-list of User defined fields in the bbdb with parameter to insert into
>       buffer")
>
> (defun get-field-name-from-character (ch)
> (cdr (assoc ch dcbl-bbdb-user-defined-fields)))

A very good idea. My temporary solution was quit inefficient. The
solution with getf is almost 15 times as fast as what I did. But your
solution is almost 2 times more efficient. Thanks.

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof


reply via email to

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