Freemarker: конвертировать строку в список с разделением, затем итерацией - PullRequest
0 голосов
/ 19 сентября 2019

Мне нужно перебрать список, созданный из встроенного «split», но мне это не удалось.

У меня есть два списка дат, которые имеют одни и те же даты.используя "seq_index_of" в качестве "Vlookup", я могу найти корреляцию и получить индекс дат, которые есть в обоих списках.однако «seq_index_of» дает вывод строки, числа, логических значений или значений даты / времени, а не последовательность (для меня, чтобы повторить).

Я использую «split», чтобы превратить строку в последовательность.Сплит не будет по-настоящему делать работу, хотя.

first i use seq_index_of for the two lists:
this will give me the index of correlated dates.
<#assign result>
<#list list1 as L1> 
${list2?seq_index_of(L1)}
</#list>
</#assign>
---------------------------------------
next I iterate the result. for this, imagine "array" is the result from above:
ex 1. this does not work. cant iterate

<#assign array = "2020-10-02,2021-10-04,2022-10-04,2023-10-04" />
<#assign mappedArray_string = []>
<#list array?split(",") as item>
<#assign mappedArray_string += [item]>
</#list>

${"is_sequence: " + mappedArray_string?is_sequence?c}

<#--print the list-->
<#list mappedArray_string as m>
<#--i cant do ?string("MM/dd/yyyy") or ant other iteration
${m?string("MM/dd/yyyy")}
<#if m?has_next>${";"}<#else>${"and"}</#if>-->
<#with no iteration, this will work. not what i need-->
${m}
</#list>
+++++++++++++++++++++++++++++++++++++++++++++
ex 2. - this works with a regular numerical list
<#assign array = [100, 200, 300, 400, 500] />
<#assign mappedArray_numbers = []>
<#list array as item>
<#assign mappedArray_numbers += [item]>
</#list>

${"is_sequence: " + mappedArray_numbers?is_sequence?c}

<#--print the list-->
<#list mappedArray_numbers as m>
${m?string("##0.0")}<#-- able to iterate-->
</#list>```

expected:
date1 ; date2 ; date3 and lastdate

error:
For "...(...)" callee: Expected a method, but this has evaluated to a string (wrapper: f.t.SimpleScalar):
==> m?string  [in template "preview-template" at line 27, column 3]

----
FTL stack trace ("~" means nesting-related):
 - Failed at: ${m?string("MM/dd/yyyy")}

1 Ответ

2 голосов
/ 20 сентября 2019

Если вы хотите перечислить элементы list2, которые также встречаются в list1, то сделайте это (требуется FreeMarker 2.3.29 из-за ?filter):

<#list list2?filter(it -> list1?seq_contains(it)) as d>
  ${d?string('MM/dd/yyyy')}
</#list>

До 2.3.29:

<#list list2 as d>
  <#if list1?seq_contains(d)>
    ${d?string('MM/dd/yyyy')}
  </#if>
</#list>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...