SWI-Promog:寻找一种在单独行中打印字典值的方法

发布于 2025-01-29 06:13:06 字数 632 浏览 2 评论 0原文

我正在寻找一种打印一些 dictionary 以每种方式的值值在终端中的单独线上。

例如,如果我的

X = abc{a:1,b:2,c:3,d:4,e:5,f:6,g:7,h:8,i:9,j:10}.

输出本身是:

?- X = abc{a:1,b:2,c:3,d:4,e:5,f:6,g:7,h:8,i:9,j:10}.
X = abc{a:1, b:2, c:3, d:4, e:5, f:6, g:7, h:8, i:9, j:10}.

为了清楚起见,我现在的目的是获取以下输出:

X = abc{
a:1, 
b:2, 
c:3, 
d:4, 
e:5, 
f:6, 
g:7, 
h:8, 
i:9, 
j:10
}.

很简单?

a:1
b:2 
c:3 
d:4
e:5 
f:6
g:7 
h:8 
i:9 
j:10

或者,它们的方法

I am searching a way to print some dictionary values in a way every value is on a separate line in the terminal.

For example if I have

X = abc{a:1,b:2,c:3,d:4,e:5,f:6,g:7,h:8,i:9,j:10}.

The output itself is:

?- X = abc{a:1,b:2,c:3,d:4,e:5,f:6,g:7,h:8,i:9,j:10}.
X = abc{a:1, b:2, c:3, d:4, e:5, f:6, g:7, h:8, i:9, j:10}.

For purpose of clarity my aim now is to get the following output:

X = abc{
a:1, 
b:2, 
c:3, 
d:4, 
e:5, 
f:6, 
g:7, 
h:8, 
i:9, 
j:10
}.

or alternatively simple

a:1
b:2 
c:3 
d:4
e:5 
f:6
g:7 
h:8 
i:9 
j:10

Is their a way to do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三月梨花 2025-02-05 06:13:06

可以提供一个自定义 portray> portray/1字典的钩子。例如:

portray(Term) :-
    is_dict(Term),
    dict_pairs(Term, Tag, Pairs),
    writef("%p{\n", [Tag]),
    foreach(member(Key-Value, Pairs), writef("\t%p: %p\n", [Key, Value])),
    write("}").

这也将在嵌套术语上使用(尽管可以正确地将更多的护理添加到缩进的嵌套术语中)。示例输出:

?- X = abc{a:1,b:2,c:3,d:4,e:5,f:6,g:7,h:8,i:9,j:foo{bar:10, baz:11}}.
X = abc{
        a: 1
        b: 2
        c: 3
        d: 4
        e: 5
        f: 6
        g: 7
        h: 8
        i: 9
        j: foo{
        bar: 10
        baz: 11
}
}.

如果钩子尚未启用以获取终端的答案(对我来说)您可能需要调整 wrath_write_options 标志。

It is possible to provide a custom portray/1 hook for dictionaries. As an example:

portray(Term) :-
    is_dict(Term),
    dict_pairs(Term, Tag, Pairs),
    writef("%p{\n", [Tag]),
    foreach(member(Key-Value, Pairs), writef("\t%p: %p\n", [Key, Value])),
    write("}").

This will also work on nested terms (though more care could be added to indent nested terms correctly). Example output:

?- X = abc{a:1,b:2,c:3,d:4,e:5,f:6,g:7,h:8,i:9,j:foo{bar:10, baz:11}}.
X = abc{
        a: 1
        b: 2
        c: 3
        d: 4
        e: 5
        f: 6
        g: 7
        h: 8
        i: 9
        j: foo{
        bar: 10
        baz: 11
}
}.

If the portray/1 hook is not already enabled for answers to the terminal (it was for me) you may need to adjust the answer_write_options flag.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文