<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Subroutine works outside but not inside For.. loop in App Development</title>
    <link>https://community.qlik.com/t5/App-Development/Subroutine-works-outside-but-not-inside-For-loop/m-p/2480125#M100658</link>
    <description>&lt;P&gt;&lt;BR /&gt;&lt;a href="https://community.qlik.com/t5/user/viewprofilepage/user-id/336"&gt;@Kushal_Chawda&lt;/a&gt;&amp;nbsp;&lt;BR /&gt;Oh for crying out loud. It's so obvious now that you say it. Most of the times when I don't get what's going on in my script it's due to automatic concatenation somewhere. Thank you very much. Solution Accepted.&lt;/P&gt;</description>
    <pubDate>Fri, 06 Sep 2024 11:23:00 GMT</pubDate>
    <dc:creator>Kellerassel</dc:creator>
    <dc:date>2024-09-06T11:23:00Z</dc:date>
    <item>
      <title>Subroutine works outside but not inside For.. loop</title>
      <link>https://community.qlik.com/t5/App-Development/Subroutine-works-outside-but-not-inside-For-loop/m-p/2480118#M100654</link>
      <description>&lt;P&gt;I have a simple subroutine:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;Sub ListFields(table_name, exclude)

Trace Excluding columns: $(exclude); 

FieldNames:
Load Concat(filtered_field_names, ', ') as filtered_field_names_concat
;
Load field_names as filtered_field_names
Where WildMatch(field_names, '$(exclude)') = 0
;
Load FieldName(IterNo(), '$(table_name)') as field_names
Autogenerate 1 While IterNo() &amp;lt;= NoOfFields('$(table_name)');


Let ListFields = Peek('filtered_field_names_concat', 0, 'FieldNames');

End Sub
&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It takes a preloaded table and lists the field names in a variable &lt;EM&gt;ListFields. &lt;/EM&gt;If you want you can pass an expression to exclude fields using a &lt;EM&gt;WildMatch().&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/EM&gt;It works:&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;example_table:
Load *
Inline [
column1, column2, exclude1, exclude2, column3
];

Let vColumnExclusion = 'exclude*'; 

Call ListFields('example_table', '$(vColumnExclusion)')

Trace Listed Columns: $(ListFields);&lt;/LI-CODE&gt;
&lt;DIV class="message default"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV class="message default"&gt;
&lt;DIV class="selectable"&gt;&lt;BR /&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Bildschirmfoto 2024-09-06 um 12.27.20.png" style="width: 400px;"&gt;&lt;img src="https://community.qlik.com/t5/image/serverpage/image-id/171371i1961EF8142B722AF/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Bildschirmfoto 2024-09-06 um 12.27.20.png" alt="Bildschirmfoto 2024-09-06 um 12.27.20.png" /&gt;&lt;/span&gt;&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But I need to do this repeatedly over the same table, with different exclusions. So I use a For..loop (simplified in my code example)&lt;BR /&gt;&lt;BR /&gt;But It doesn't work in a For.. loop.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;For i=-1 to 0

Let vColumnExclusion = If($(i), 'column*', 'exclude*'); 

Call ListFields('example_table', '$(vColumnExclusion)')

Trace $(ListFields);

Next i&lt;/LI-CODE&gt;
&lt;P&gt;&lt;BR /&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Bildschirmfoto 2024-09-06 um 12.28.33.png" style="width: 400px;"&gt;&lt;img src="https://community.qlik.com/t5/image/serverpage/image-id/171372i7B9AD096C9AFFBE4/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Bildschirmfoto 2024-09-06 um 12.28.33.png" alt="Bildschirmfoto 2024-09-06 um 12.28.33.png" /&gt;&lt;/span&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;EM&gt;vColumnExclusion &lt;/EM&gt;is correctly assigned and passed to the subroutine as confirms the &lt;EM&gt;Trace &lt;/EM&gt;inside the sub.&lt;BR /&gt;&lt;BR /&gt;&lt;STRONG&gt;But&amp;nbsp;&lt;EM&gt;ListFields &lt;/EM&gt;only every contains the result set of the first exclusion &lt;EM&gt;"column*". &lt;/EM&gt;&lt;/STRONG&gt;The second&amp;nbsp;&lt;EM&gt;vColumnExclusion&lt;/EM&gt; is passed but not executed.&lt;BR /&gt;&lt;BR /&gt;What is going on? Any hint, explanation or even solution would be greatly appreciated.&lt;/P&gt;</description>
      <pubDate>Fri, 06 Sep 2024 10:37:43 GMT</pubDate>
      <guid>https://community.qlik.com/t5/App-Development/Subroutine-works-outside-but-not-inside-For-loop/m-p/2480118#M100654</guid>
      <dc:creator>Kellerassel</dc:creator>
      <dc:date>2024-09-06T10:37:43Z</dc:date>
    </item>
    <item>
      <title>Re: Subroutine works outside but not inside For.. loop</title>
      <link>https://community.qlik.com/t5/App-Development/Subroutine-works-outside-but-not-inside-For-loop/m-p/2480124#M100657</link>
      <description>&lt;P&gt;&lt;a href="https://community.qlik.com/t5/user/viewprofilepage/user-id/155989"&gt;@Kellerassel&lt;/a&gt;&amp;nbsp; It seems to be working as expected as it is generating two records for two conditions&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-left" image-alt="Screenshot 2024-09-06 at 11.59.06.png" style="width: 200px;"&gt;&lt;img src="https://community.qlik.com/t5/image/serverpage/image-id/171373i31FC3A46BDD66152/image-size/small?v=v2&amp;amp;px=200" role="button" title="Screenshot 2024-09-06 at 11.59.06.png" alt="Screenshot 2024-09-06 at 11.59.06.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There is a reason it is not displaying correct value in trace statement, because of below statement&lt;/P&gt;
&lt;P&gt;Let ListFields = Peek('filtered_field_names_concat', &lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;0&lt;/STRONG&gt;&lt;/FONT&gt;, 'FieldNames');&lt;/P&gt;
&lt;P&gt;When first loop runs, it shows correct value in trace because in&amp;nbsp;FieldNames table there is only one record. In next loop there are two records in&amp;nbsp;FieldNames table, but above Peek statement has static parameter of "&lt;FONT color="#FF0000"&gt;0&lt;FONT color="#000000"&gt;"&amp;nbsp;which displays first record only.&amp;nbsp; &lt;STRONG&gt;Parameter of 0 in peek represents first record of the table.&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;&lt;FONT color="#000000"&gt;To show the correct value in Trace you need to change the parameter of the Peek function dynamically for which little bit of code change will be required like below&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="comic sans ms,sans-serif"&gt;example_table:&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="comic sans ms,sans-serif"&gt;Load *&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="comic sans ms,sans-serif"&gt;Inline [&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="comic sans ms,sans-serif"&gt;column1, column2, exclude1, exclude2, column3&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="comic sans ms,sans-serif"&gt;];&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;&lt;FONT face="comic sans ms,sans-serif"&gt;Sub ListFields(table_name, exclude, &lt;FONT color="#FF0000"&gt;iteration&lt;/FONT&gt;)&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="comic sans ms,sans-serif"&gt;Trace Excluding columns: $(exclude);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="comic sans ms,sans-serif"&gt;FieldNames:&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="comic sans ms,sans-serif"&gt;Load Concat(filtered_field_names, ', ') as filtered_field_names_concat&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="comic sans ms,sans-serif"&gt;;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="comic sans ms,sans-serif"&gt;Load field_names as filtered_field_names&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="comic sans ms,sans-serif"&gt;Where WildMatch(field_names, '$(exclude)') = 0&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="comic sans ms,sans-serif"&gt;;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="comic sans ms,sans-serif"&gt;Load FieldName(IterNo(), '$(table_name)') as field_names&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="comic sans ms,sans-serif"&gt;Autogenerate 1 While IterNo() &amp;lt;= NoOfFields('$(table_name)');&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;&lt;FONT face="comic sans ms,sans-serif"&gt;Let ListFields = Peek('filtered_field_names_concat', &lt;FONT color="#FF0000"&gt;$(iteration)&lt;/FONT&gt;, 'FieldNames');&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="comic sans ms,sans-serif"&gt;End Sub&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;&lt;FONT face="comic sans ms,sans-serif"&gt;For i=-1 to 0&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="comic sans ms,sans-serif"&gt;Let vColumnExclusion = If($(i), 'column*', 'exclude*');&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="comic sans ms,sans-serif"&gt;Call ListFields('example_table', '$(vColumnExclusion)',&lt;FONT color="#FF0000"&gt;$(i)+1&lt;/FONT&gt;);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="comic sans ms,sans-serif"&gt;Trace $(ListFields);&lt;/FONT&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;FONT face="comic sans ms,sans-serif"&gt;Next i&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Screenshot 2024-09-06 at 11.57.57.png" style="width: 400px;"&gt;&lt;img src="https://community.qlik.com/t5/image/serverpage/image-id/171374i2A6EFB5A02681299/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Screenshot 2024-09-06 at 11.57.57.png" alt="Screenshot 2024-09-06 at 11.57.57.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 06 Sep 2024 11:10:53 GMT</pubDate>
      <guid>https://community.qlik.com/t5/App-Development/Subroutine-works-outside-but-not-inside-For-loop/m-p/2480124#M100657</guid>
      <dc:creator>Kushal_Chawda</dc:creator>
      <dc:date>2024-09-06T11:10:53Z</dc:date>
    </item>
    <item>
      <title>Re: Subroutine works outside but not inside For.. loop</title>
      <link>https://community.qlik.com/t5/App-Development/Subroutine-works-outside-but-not-inside-For-loop/m-p/2480125#M100658</link>
      <description>&lt;P&gt;&lt;BR /&gt;&lt;a href="https://community.qlik.com/t5/user/viewprofilepage/user-id/336"&gt;@Kushal_Chawda&lt;/a&gt;&amp;nbsp;&lt;BR /&gt;Oh for crying out loud. It's so obvious now that you say it. Most of the times when I don't get what's going on in my script it's due to automatic concatenation somewhere. Thank you very much. Solution Accepted.&lt;/P&gt;</description>
      <pubDate>Fri, 06 Sep 2024 11:23:00 GMT</pubDate>
      <guid>https://community.qlik.com/t5/App-Development/Subroutine-works-outside-but-not-inside-For-loop/m-p/2480125#M100658</guid>
      <dc:creator>Kellerassel</dc:creator>
      <dc:date>2024-09-06T11:23:00Z</dc:date>
    </item>
  </channel>
</rss>

