Reputation: 2111
I need some help with the format
function and arrays.
My objective is to print a 2 dimensional array of N·N integer values as N integers per line. For example:
#2A((1 2 3)
(4 5 6)
(7 8 9))
should be printed as
1 2 3
4 5 6
7 8 9
I couldn't find any documentation on how to use format
to print arrays. Can it actually be done, or should I convert my array into a list and use something like:
(format t "~{~%~{~A~^ ~}~}" list)
Upvotes: 4
Views: 1638
Reputation: 3170
(defun show-board (board)
(loop for i below (car (array-dimensions board)) do
(loop for j below (cadr (array-dimensions board)) do
(let ((cell (aref board i j)))
(format t "~a " cell)))
(format t "~%")))
Upvotes: 4
Reputation: 51511
If I am not mistaken, there is no direct way for format
to go "into" an array. You could write your own function, to be used by tilde-slash (~/function/
, see CLHS), or you could coerce the array to a list and use either the directives you proposed, or ~/pprint-tabular/
. If you want to define your own, the CLHS has example code for pprint-tabular
which you could modify for arrays.
Upvotes: 4