From: ·······@erols.com
Subject: Question about setf and read
Date: 
Message-ID: <344E7449.46B4B6B4@erols.com>
I am working on a simple program to calculate the net damage of multiple
hits for a space combat board game that I enjoy playing, but takes a
while to do manually. I want the user to be able to define the
characterestics of the ships that he's going to use. I can do all of
that for one user-defined ship, but I can't figure out how to define a
list/array based on user input (ie if the ship is named 'Falcon', the
user types in falcon and the program assigns the name of the array with
its stats to be Falcon.). I have tried using read in combination with
setf and make-array, but I can't seem to find a combination that works.
If at all possible a simple answer would be appreciated (since this is a
relatively basic program, I don't want to try my hand at classes and
such, I only program as a hobby, I don't really have the time right now
to learn the whole language). Anyway please e-mail me any responses.
Thanks in advance.
········@erols.com
From: Barry Margolin
Subject: Re: Question about setf and read
Date: 
Message-ID: <62t8rj$lg6@pasilla.bbnplanet.com>
In article <·················@erols.com>,  <·······@erols.com> wrote:
>I am working on a simple program to calculate the net damage of multiple
>hits for a space combat board game that I enjoy playing, but takes a
>while to do manually. I want the user to be able to define the
>characterestics of the ships that he's going to use. I can do all of
>that for one user-defined ship, but I can't figure out how to define a
>list/array based on user input (ie if the ship is named 'Falcon', the
>user types in falcon and the program assigns the name of the array with
>its stats to be Falcon.). I have tried using read in combination with
>setf and make-array, but I can't seem to find a combination that works.
>If at all possible a simple answer would be appreciated (since this is a
>relatively basic program, I don't want to try my hand at classes and
>such, I only program as a hobby, I don't really have the time right now
>to learn the whole language). Anyway please e-mail me any responses.
>Thanks in advance.

Your description isn't extremely clear, but I think what you're trying to
do would be:

(defun get-ship ()
  (format t "~&Enter ship name: ")
  (let ((ship-name (read)))
    (setf (symbol-value ship-name) (make-ship :name ship-name))))

This is generally considered to be bad style these days.  A better approach
would be something like:

(defvar *ship-table* (make-hash-table :test #'equal)) ; or #'equalp

(defun get-ship ()
  (format t "~&Enter ship name: ")
  (let ((ship-name (read-line)))
    (setf (gethash ship-name *ship-table*) (make-ship :name ship-name))))

I'm not sure what you were referring to with "array with its stats"; if you
don't want to use classes, you should at least use DEFSTRUCT to define a
structure to contain the ship info, not use an array (arrays are generally
for uniform data indexed numerically, not for abstract data types).

-- 
Barry Margolin, ······@bbnplanet.com
GTE Internetworking, Powered by BBN, Cambridge, MA
Support the anti-spam movement; see <http://www.cauce.org/>
Please don't send technical questions directly to me, post them to newsgroups.