From: kevin lai
Subject: a fundamental array question
Date: 
Message-ID: <3adcc3f9$0$3703@wodc7nh7.news.uu.net>
How to create a copy of an array that I can manipulate the elements without
changing the origional array? Any suggestions are highly appreciated,

Kevin

From: Kent M Pitman
Subject: Re: a fundamental array question
Date: 
Message-ID: <sfwsnj79m7u.fsf@world.std.com>
"kevin lai" <····@utrc.utc.com> writes:

> How to create a copy of an array that I can manipulate the elements without
> changing the origional array? Any suggestions are highly appreciated,

There's no built-in for doing this.  At minimum you have to make a new 
array with the same shape and with any other attributes you care about
chosen according to how they were in the previous array.  But when you
say "manipulate" you might mean mutate, and if by that you mean things
like you want to do
  (let ((a (my-copy-array b)))
    (setf (car (aref a 0)) ...))
without changing the car of (aref b 0), then you'll have to do more work
of your own devising.  Note that for the simple case of wanting to get an
array of the same pointers as the ohter array, the :initial-contents argument
to make-array can be quite helpful.
From: kevin lai
Subject: Re: a fundamental array question
Date: 
Message-ID: <3adcd0b4$0$3704@wodc7nh7.news.uu.net>
Thanks for replying my question.

The "manipulate" in previous e-mail meant only to change the values of
elements in the copy (supposed to be exact the same as the origional copy).

For example,

(setq a (make-array '(2 3)
    :element-type 'string
    :initial-contents
    '(("a" "b" "c")
      ("d" "e" "f"))))

(setq b a)
(setf (aref b 0 0) "x"))

(aref a 0 0) --> "x" (which is I want to avoid).

You mentioned that it may need to do some devising, can you please elaborate
a bit more on what need to be done to make this happen.

Thank you very much,
Kevin


Kent M Pitman wrote in message ...
>"kevin lai" <····@utrc.utc.com> writes:
>
>> How to create a copy of an array that I can manipulate the elements
without
>> changing the origional array? Any suggestions are highly appreciated,
>
>There's no built-in for doing this.  At minimum you have to make a new
>array with the same shape and with any other attributes you care about
>chosen according to how they were in the previous array.  But when you
>say "manipulate" you might mean mutate, and if by that you mean things
>like you want to do
>  (let ((a (my-copy-array b)))
>    (setf (car (aref a 0)) ...))
>without changing the car of (aref b 0), then you'll have to do more work
>of your own devising.  Note that for the simple case of wanting to get an
>array of the same pointers as the ohter array, the :initial-contents
argument
>to make-array can be quite helpful.
From: Kent M Pitman
Subject: Re: a fundamental array question
Date: 
Message-ID: <sfwhezn3wxv.fsf@world.std.com>
"kevin lai" <····@utrc.utc.com> writes:

> 
> Thanks for replying my question.
> 
> The "manipulate" in previous e-mail meant only to change the values of
> elements in the copy (supposed to be exact the same as the origional copy).
> 
> For example,
> 
> (setq a (make-array '(2 3)
>     :element-type 'string
>     :initial-contents
>     '(("a" "b" "c")
>       ("d" "e" "f"))))
> 
> (setq b a)
> (setf (aref b 0 0) "x"))
> 
> (aref a 0 0) --> "x" (which is I want to avoid).
> 
> You mentioned that it may need to do some devising, can you please elaborate
> a bit more on what need to be done to make this happen.

You need something vaguely like the following, which I whipped off kind
of quickly and might have some bugs but should give you the basic idea...

 (defun copy-array (a) 
   (let* ((dims (array-dimensions a))
          (copy (make-array dims
                   ;; NOTE: doesn't copy displacement info
                   :element-type (array-element-type a) 
                   :adjustable (adjustable-array-p a)
                   :fill-pointer (and (array-has-fill-pointer-p a)
                                      (fill-pointer a)))))
     (dotimes (i (apply #'* dims)) 
       (setf (row-major-aref copy i)
             (row-major-aref a    i)))
     copy))
From: kevin lai
Subject: Re: a fundamental array question
Date: 
Message-ID: <3add8e76$0$9965@wodc7nh7.news.uu.net>
Thank you so much, it works without any changes.

Kevin


Kent M Pitman wrote in message ...
>"kevin lai" <····@utrc.utc.com> writes:
>
>>
>> Thanks for replying my question.
>>
>> The "manipulate" in previous e-mail meant only to change the values of
>> elements in the copy (supposed to be exact the same as the origional
copy).
>>
>> For example,
>>
>> (setq a (make-array '(2 3)
>>     :element-type 'string
>>     :initial-contents
>>     '(("a" "b" "c")
>>       ("d" "e" "f"))))
>>
>> (setq b a)
>> (setf (aref b 0 0) "x"))
>>
>> (aref a 0 0) --> "x" (which is I want to avoid).
>>
>> You mentioned that it may need to do some devising, can you please
elaborate
>> a bit more on what need to be done to make this happen.
>
>You need something vaguely like the following, which I whipped off kind
>of quickly and might have some bugs but should give you the basic idea...
>
> (defun copy-array (a)
>   (let* ((dims (array-dimensions a))
>          (copy (make-array dims
>                   ;; NOTE: doesn't copy displacement info
>                   :element-type (array-element-type a)
>                   :adjustable (adjustable-array-p a)
>                   :fill-pointer (and (array-has-fill-pointer-p a)
>                                      (fill-pointer a)))))
>     (dotimes (i (apply #'* dims))
>       (setf (row-major-aref copy i)
>             (row-major-aref a    i)))
>     copy))