格式 - 帮助打印表格 [英] format - Help with printing a table
问题描述
基本上我想要的do是类似于
$ p $ (format t〜{|〜{〜5d〜} |〜%〜}'(( 1 23 2 312)(23 456 1 7890)))
它应该从列表(从任何嵌套列表中的最长元素的长度+ 1)计算出来,就像
| 1 23 2 312 |
| 23 456 1 7890 |
也许我在这里想的太复杂了,还有更简单的方法来做我想做的事情,但是我想我跑进了一个我不能离开的心理角落。
宽度必须为 width ,那么你可以这样做:格式t〜{|〜{〜Vd〜} |〜%〜}width((1 23 2 312)(23 456 1 7890)))
$ b $
5
已被 V
和<$ c已添加$ c> width 作为 FORMAT
/
<编辑:原来的答案没有正确说明嵌套的指令
格式控制字符串 V
你可以试试这个:
(setf width 5)
(setf da (格式t〜{〜{〜{〜Vd〜}〜} |〜%〜} $ b((1 23 2 312)(23 456 1 7890)))
$ b(mapcar#'(lambda(r)(mapcar#'(lambda(v)(list width v))r))data))
这个格式字符串需要在每个值之前的期望宽度。 (mapcar ...)
表达式完成了这个。
This question will probably end in a facepalm, but I've tried for a while and am still stuck despite reading through the hyperspec.
Basically what I want to do is something like
(format t "~{|~{ ~5d~}|~%~}" '((1 23 2 312) (23 456 1 7890)))
but instead of hard-coding the 5 it should be calculated from the list (length of longest element from any nested list + 1) to give something like
| 1 23 2 312|
| 23 456 1 7890|
Maybe I'm thinking way too complicated here and there is an easier way to do what I want, but I think I ran myself into a mental corner that I can't get out of.
Assuming the required width is bound to width
, then you can do this:
(format t "~{|~{ ~Vd~}|~%~}" width '((1 23 2 312) (23 456 1 7890)))
5
has been replaced by V
and width
has been added as an argument to FORMAT
/
edit: original answer did not correctly account for the nested directives
In a format control string V
may be used in place of any constant value, indicating that the corresponding value is to be taken from the argument list instead.
You can try this:
(setf width 5)
(setf data '((1 23 2 312) (23 456 1 7890)))
(format t "~{|~{ ~{~Vd~}~}|~%~}"
(mapcar #'(lambda (r) (mapcar #'(lambda (v) (list width v)) r)) data) )
This format string requires the desired width to precede each value. The (mapcar ...)
expression accomplishes this.
这篇关于格式 - 帮助打印表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!