gpt4 book ai didi

perl - 使用模板工具包从列表中过滤项目

转载 作者:行者123 更新时间:2023-12-02 20:45:55 25 4
gpt4 key购买 nike

如何过滤 Template Toolkit 中动态生成的列表中的某些项目?我有一个 id 列表(也是动态生成的)和一个要排除的 id 列表,我需要只获取未排除的 id。最好的方法是什么?示例代码:

[% SET ids = [1,2,4,10,11,12,13,17,19,20,21,50,51] %]
[% SET id_excluded = [10,11,13,20,50] %]
[% FOREACH pid IN ids %]
[% IF ?code to filter the ids? %]
[% pid %]
[% END %]
[% END %]

最佳答案

尝试使用 grep VMethod 来处理列表,例如:

[% SET ids = [1,2,4,10,11,12,13,17,19,20,21,50,51] %]
[% SET id_excluded = [10,11,13,20,50] %]
[% FOREACH pid IN ids %]
[% UNLESS id_excluded.grep("^$pid\$").size %]
[% pid %]
[% END %]
[% END %]

产生以下结果:

1 2 4 12 17 19 21 51

关于perl - 使用模板工具包从列表中过滤项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7031247/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com