From: Stefan Sonesson
Subject: How to get a specific attribute from a struct?
Date: 
Message-ID: <373DBC63.1E33E@swipnet.se>
Hello,

I wan't to look up specific values from a struct.

(defstruct book
  subject
  title
  author
  publisher
  year)

(setq booka (make-book :subject "C LISP"
                                                 :title "Common LISP:The
language"
                                                 :author "Guy L. Steele
Jr."
                                                 :publisher "Author"
                                                 :year "1995"))
(setq bookb (...
.
.aso.

One of my problems are that I am a complete (50 hrs or so) newbie.
I know it is possible to print the whole structure, but if I need to
search for a specific subject,title,author,publisher or year(to retrieve
all info on a book) without knowing all of them thats not adequate.

If you know a faq that covers this please point me there.

Thank you for your time.

/Stefan

From: Lars Marius Garshol
Subject: Re: How to get a specific attribute from a struct?
Date: 
Message-ID: <wkhfpe9olj.fsf@ifi.uio.no>
* Stefan Sonesson
| 
| (defstruct book
|   subject
|   title
|   author
|   publisher
|   year)

This defines the functions book-subject, book-title, book-author,
book-publisher and book-year, which you can use to access the contents
of the struct. (With setf you can also assign to them.)
 
[······@birk105 corba]$ clisp -q

[1]> (defstruct book
   subject
   title
   author
   publisher
   year)
BOOK
[2]> (setq booka (make-book :subject "C LISP"))
#S(BOOK :SUBJECT "C LISP" :TITLE NIL :AUTHOR NIL :PUBLISHER NIL :YEAR NIL)
[3]> (book-subject booka)
"C LISP"
[4]> (setf (book-subject booka) "Common Lisp")
"Common Lisp"
[5]> (book-subject booka)
"Common Lisp"

| If you know a faq that covers this please point me there.

Any good language introduction should cover this stuff, and would very
likely be a better place to turn to than a FAQ.

--Lars M.
From: Kent M Pitman
Subject: Re: How to get a specific attribute from a struct?
Date: 
Message-ID: <sfwvhduqioa.fsf@world.std.com>
Stefan Sonesson <···············@swipnet.se> writes:

> I wan't to look up specific values from a struct.
> 
> (defstruct book
>   subject
>   title
>   author
>   publisher
>   year)
> 
> (setq booka (make-book :subject "C LISP"
>                                                  :title "Common LISP:The
> language"
>                                                  :author "Guy L. Steele
> Jr."
>                                                  :publisher "Author"
>                                                  :year "1995"))
> (setq bookb (...
> .
> .aso.
> 
> One of my problems are that I am a complete (50 hrs or so) newbie.
> I know it is possible to print the whole structure, but if I need to
> search for a specific subject,title,author,publisher or year(to retrieve
> all info on a book) without knowing all of them thats not adequate.

First, you should know you don't have to put each in a variable.  You can
make a data structure to hold these so that you can search it.  e.g.,
 (defvar *books* (list (make-book ...) (make-book ...) ...))
or you can programmatically maintain the book list with:
 (defvar *books* '())
 (defmacro define-book (title &rest other-keys)
   `(push (make-book :title ,title ,@other-keys) *books*))
The accessors book-subject, book-title, etc. will get you the parts of
the book.  You can find a book by testing those fields as in:
 (find "Guy L. Steele Jr." *books* :key #'book-author :test #'string-equal)
That will find the first one.  You might want to find several, as in:
 (loop for book in *books*
       when (and (>= (read-from-string (book-year book)) 1990)
                 (search "Steele" (book-author book)))
       collect book)
which will get you a list of all Steele's books since 1990.  Incidentally,
I'd store the year as a number to make the read-from-string unnecessary
in searches.  Also, Steele's most recent edition of CLTL (the second
edition) was published in 1990, not 1995.  My search example here has
the year adjusted to be tolerant so should still work when ou correct
your hypothetical book database.
From: Stefan Sonesson
Subject: Re: How to get a specific attribute from a struct?
Date: 
Message-ID: <373DD061.3AB67C30@swipnet.se>
Good evening,

I was much helped by your answer, and I have changed the year of CLTL too.
I noticed your name was in the preface so I bow and say thank you.

Have a nice day,

/Stefan