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.
"Jonathon McKitrick" <···········@bigfoot.com> wrote in message
·····························@m73g2000cwd.googlegroups.com...
>
> 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.
CL-USER 4 >
(apply #'nconc '( ("property1" "val1") ("property2" "val2")))
("property1" "val1" "property2" "val2")
Carl Taylor
"Jonathon McKitrick" <···········@bigfoot.com> writes:
> 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.
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.
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" <···········@bigfoot.com> writes:
> 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?
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.
"Jonathon McKitrick" <···········@bigfoot.com> writes:
> 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?
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.
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?