Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Best way to convert query to property list

0 views
Skip to first unread message

Jonathon McKitrick

unread,
Jul 4, 2006, 1:33:27 PM7/4/06
to

I know this has to be simple, but the approaches I'm trying just aren't
working for some reason.

I'm querying a db and getting a list of 2-item lists of properties:

( ("property1" "val1") ("property2" "val2") ...)

What I want is either a hash, plist, or alist, whatever makes the most
sense for a throwaway result that will let me get the value for any
property but not save the whole structure when I'm done with it. It
seems a hash would be overkill, but I could be wrong.

I thought this would create the plist I wanted, but I was wrong:

(loop for prop in props collect (mapcar #'append prop))

It just gives me the exact same list as went into it.

Carl Taylor

unread,
Jul 4, 2006, 1:45:44 PM7/4/06
to

"Jonathon McKitrick" <j_mck...@bigfoot.com> wrote in message
news:1152034407.8...@m73g2000cwd.googlegroups.com...


CL-USER 4 >
(apply #'nconc '( ("property1" "val1") ("property2" "val2")))
("property1" "val1" "property2" "val2")

Carl Taylor

Pascal Bourguignon

unread,
Jul 4, 2006, 1:51:10 PM7/4/06
to
"Jonathon McKitrick" <j_mck...@bigfoot.com> writes:

You already have an a-list.

(second (assoc "property1" '(("property1" "val1") ("property2" "val2") )
:test (function string=)))
--> "val1"


--
__Pascal Bourguignon__ http://www.informatimago.com/

Nobody can fix the economy. Nobody can be trusted with their finger
on the button. Nobody's perfect. VOTE FOR NOBODY.

Jonathon McKitrick

unread,
Jul 4, 2006, 2:57:14 PM7/4/06
to

Pascal Bourguignon wrote:
> You already have an a-list.
>
> (second (assoc "property1" '(("property1" "val1") ("property2" "val2") )
> :test (function string=)))
> --> "val1"

I knew in was simple, but not that simple!!

I thought cons cells had to be specifically created as such?

Jonathon McKitrick

unread,
Jul 4, 2006, 2:59:55 PM7/4/06
to

Pascal Bourguignon wrote:
> You already have an a-list.
>
> (second (assoc "property1" '(("property1" "val1") ("property2" "val2") )
> :test (function string=)))
> --> "val1"

I knew in was simple, but not that simple!!

Pascal Bourguignon

unread,
Jul 4, 2006, 3:06:35 PM7/4/06
to
"Jonathon McKitrick" <j_mck...@bigfoot.com> writes:

Lists are made of cons cells.

("property1" "val1") == ("property1" . ( "val1" . () ))

Usually, you get the value of an assoc with CDR:

(cdr (assoc key a-list))

that is, if you had a-list == ((prop1 . val1) (prop2 . val2) ...)


But since your couples are stored in lists (eg 2 cons cells instead of
one cons cell), we must use CADR (= SECOND) instead of CDR, that's all.

If we still used CDR, we'd simply get the list ("val1") instead of "val1".

--
__Pascal Bourguignon__ http://www.informatimago.com/

NOTE: The most fundamental particles in this product are held
together by a "gluing" force about which little is currently known
and whose adhesive power can therefore not be permanently
guaranteed.

Ari Johnson

unread,
Jul 4, 2006, 3:17:32 PM7/4/06
to
"Jonathon McKitrick" <j_mck...@bigfoot.com> writes:

Just to expound on what Pascal has already said:

(cons (cons "property1" (cons "val1" '()))
(cons (cons "property2" (cons "val2" '()))
'()))
==>


(("property1" "val1") ("property2" "val2"))

The cons cells were created as such by whatever gave you that list of
lists to begin with, so you were correct.

0 new messages