<?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 Follow Up Question: Parse Text / Custom Code in Qlik Automate</title>
    <link>https://community.qlik.com/t5/Qlik-Automate/Follow-Up-Question-Parse-Text-Custom-Code/m-p/2116123#M2452</link>
    <description>&lt;P&gt;Hej all,&lt;/P&gt;
&lt;P&gt;with regards to &lt;A href="https://community.qlik.com/t5/Qlik-Application-Automation/Parse-text-RegEx-ParseAll/m-p/2060934#M1684" target="_self"&gt;this post&lt;/A&gt; (thanks again &lt;a href="https://community.qlik.com/t5/user/viewprofilepage/user-id/134179"&gt;@AfeefaTk&lt;/a&gt;&amp;nbsp;), i am running into some difficulties, when trying to parse the same text (same file actually) when i fetch it from a s3 bucket (vs from gitlab).&lt;/P&gt;
&lt;P&gt;To sum up, here is what i am after:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;I have a given file (.qvs) in my s3 bucket. The file can contain the following content:&lt;BR /&gt;&lt;BR /&gt;
&lt;DIV&gt;&lt;LI-CODE lang="csharp"&gt;trace start tab: about;

	// 	#############################################################################################################################
	//	
	//	Some Comments...
	//	
	// 	#############################################################################################################################

trace end tab: about;

trace start tab: execute;

	// 	#############################################################################################################################
	//	
	//	Some Comments...
	//	
	// 	#############################################################################################################################

	//=========================================================================================
	Trace  ********* started: MOD_Table_1.qvs 
    //=========================================================================================
	$(Must_Include=$(vModulesRoot)03_REVENUE\MOD_Table_1.qvs);
	call  StoreIntoSDL ('MOD', 'Table_1', '03_REVENUE', 'Table_1');
	call 	CreateMetaData ('Table_1', 'GEN_MOD_Table_1.qvs', 'MOD_Table_1.qvs', '03_REVENUE');
	
	
	
  	//=========================================================================================
	Trace  ********* started: MOD_Table_2.qvs 
    //=========================================================================================
	$(Must_Include=$(vModulesRoot)03_REVENUE\MOD_Table_2.qvs);
	call StoreIntoSDL ('MOD', 'Table_2', '03_REVENUE', 'Table_2');
	call CreateMetaData ('Table_2', 'GEN_MOD_Table_1.qvs', 'MOD_Table_2.qvs', '03_REVENUE');
	
	
trace end tab: execute;
&lt;/LI-CODE&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;P&gt;The important pieces are the includes and the StoreToSDL function.&amp;nbsp;A given file can contain n occurances of includes and StoreToSDLs.&amp;nbsp;&lt;/P&gt;
&lt;/LI&gt;
&lt;LI&gt;In the referred post the following custom code was suggested:&lt;BR /&gt;&lt;BR /&gt;&lt;LI-CODE lang="javascript"&gt;$gen = $inputs['gen'];
// Extract include statements
$include_statements = array();
preg_match_all('/Must_Include=\$\(vModulesRoot\)([^\)]+)/', $gen, $matches);
foreach ($matches[1] as $match) {
    $include_statements[] = $match;
}

// Extract StoreIntoSDL lines following include statements
$store_into_sdl = array();
preg_match_all('/call StoreIntoSDL \((.+?)\);/', $gen, $matches);
foreach ($matches[0] as $match) {
    $store_into_sdl[] = $match;
}

// Combine include statements and StoreIntoSDL lines into a JSON object
$json_array = array();
for ($i = 0; $i &amp;lt; count($include_statements); $i++) {
    $json_array[] = array(
        'include' =&amp;gt; $include_statements[$i],
        'store_into_sdl' =&amp;gt; $store_into_sdl[$i]
    );
}

// Convert JSON object to JSON string
echo json_encode($json_array);​&lt;/LI-CODE&gt;
&lt;P&gt;&lt;BR /&gt;This worked fine, when the file came from gitlab and i decoded64 its content. However, when the file is coming from a s3 bucket, my automation failes wit the following error:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;Fatal error: Uncaught TypeError: preg_match_all(): Argument #2 ($subject) must be of type string, array given in /tmp/code:5
Stack trace:
#0 /tmp/code(5): preg_match_all('/Must_Include=\\...', Array, NULL)
#1 {main}
  thrown in /tmp/code on line 5&lt;/LI-CODE&gt;&lt;/LI&gt;
&lt;LI&gt;
&lt;P&gt;The expected outcome (and maybe this can be achieved by going a different path...like not using custom code ?)&amp;nbsp; is that, while iterating through the file, i am getting the following output:&lt;/P&gt;
&lt;P&gt;1st iteration:&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;vMod = 03_TRANSFORM/03_REVENUE/MOD_Table_1.qvs&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;vStore =&amp;nbsp;call StoreIntoSDL ('MOD', 'Table_1', '03_REVENUE', 'Table_1');&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;2nd iteration&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;vMod = 03_TRANSFORM/03_REVENUE/MOD_Table_2.qvs&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;vStore =&amp;nbsp;call StoreIntoSDL ('MOD', 'Table_2', '03_REVENUE', 'Table_2');&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;and so on...&amp;nbsp;&lt;/P&gt;
&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have out together a sample automation, just to get the idea. (For some reason, it runs into an error, but you should get the idea of what i am after)&lt;/P&gt;
&lt;P&gt;I appreciate every hint and help!&amp;nbsp; Thanks in advance!&lt;/P&gt;</description>
    <pubDate>Wed, 13 Sep 2023 05:11:31 GMT</pubDate>
    <dc:creator>xyz_1011</dc:creator>
    <dc:date>2023-09-13T05:11:31Z</dc:date>
    <item>
      <title>Follow Up Question: Parse Text / Custom Code</title>
      <link>https://community.qlik.com/t5/Qlik-Automate/Follow-Up-Question-Parse-Text-Custom-Code/m-p/2116123#M2452</link>
      <description>&lt;P&gt;Hej all,&lt;/P&gt;
&lt;P&gt;with regards to &lt;A href="https://community.qlik.com/t5/Qlik-Application-Automation/Parse-text-RegEx-ParseAll/m-p/2060934#M1684" target="_self"&gt;this post&lt;/A&gt; (thanks again &lt;a href="https://community.qlik.com/t5/user/viewprofilepage/user-id/134179"&gt;@AfeefaTk&lt;/a&gt;&amp;nbsp;), i am running into some difficulties, when trying to parse the same text (same file actually) when i fetch it from a s3 bucket (vs from gitlab).&lt;/P&gt;
&lt;P&gt;To sum up, here is what i am after:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;I have a given file (.qvs) in my s3 bucket. The file can contain the following content:&lt;BR /&gt;&lt;BR /&gt;
&lt;DIV&gt;&lt;LI-CODE lang="csharp"&gt;trace start tab: about;

	// 	#############################################################################################################################
	//	
	//	Some Comments...
	//	
	// 	#############################################################################################################################

trace end tab: about;

trace start tab: execute;

	// 	#############################################################################################################################
	//	
	//	Some Comments...
	//	
	// 	#############################################################################################################################

	//=========================================================================================
	Trace  ********* started: MOD_Table_1.qvs 
    //=========================================================================================
	$(Must_Include=$(vModulesRoot)03_REVENUE\MOD_Table_1.qvs);
	call  StoreIntoSDL ('MOD', 'Table_1', '03_REVENUE', 'Table_1');
	call 	CreateMetaData ('Table_1', 'GEN_MOD_Table_1.qvs', 'MOD_Table_1.qvs', '03_REVENUE');
	
	
	
  	//=========================================================================================
	Trace  ********* started: MOD_Table_2.qvs 
    //=========================================================================================
	$(Must_Include=$(vModulesRoot)03_REVENUE\MOD_Table_2.qvs);
	call StoreIntoSDL ('MOD', 'Table_2', '03_REVENUE', 'Table_2');
	call CreateMetaData ('Table_2', 'GEN_MOD_Table_1.qvs', 'MOD_Table_2.qvs', '03_REVENUE');
	
	
trace end tab: execute;
&lt;/LI-CODE&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;P&gt;The important pieces are the includes and the StoreToSDL function.&amp;nbsp;A given file can contain n occurances of includes and StoreToSDLs.&amp;nbsp;&lt;/P&gt;
&lt;/LI&gt;
&lt;LI&gt;In the referred post the following custom code was suggested:&lt;BR /&gt;&lt;BR /&gt;&lt;LI-CODE lang="javascript"&gt;$gen = $inputs['gen'];
// Extract include statements
$include_statements = array();
preg_match_all('/Must_Include=\$\(vModulesRoot\)([^\)]+)/', $gen, $matches);
foreach ($matches[1] as $match) {
    $include_statements[] = $match;
}

// Extract StoreIntoSDL lines following include statements
$store_into_sdl = array();
preg_match_all('/call StoreIntoSDL \((.+?)\);/', $gen, $matches);
foreach ($matches[0] as $match) {
    $store_into_sdl[] = $match;
}

// Combine include statements and StoreIntoSDL lines into a JSON object
$json_array = array();
for ($i = 0; $i &amp;lt; count($include_statements); $i++) {
    $json_array[] = array(
        'include' =&amp;gt; $include_statements[$i],
        'store_into_sdl' =&amp;gt; $store_into_sdl[$i]
    );
}

// Convert JSON object to JSON string
echo json_encode($json_array);​&lt;/LI-CODE&gt;
&lt;P&gt;&lt;BR /&gt;This worked fine, when the file came from gitlab and i decoded64 its content. However, when the file is coming from a s3 bucket, my automation failes wit the following error:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;Fatal error: Uncaught TypeError: preg_match_all(): Argument #2 ($subject) must be of type string, array given in /tmp/code:5
Stack trace:
#0 /tmp/code(5): preg_match_all('/Must_Include=\\...', Array, NULL)
#1 {main}
  thrown in /tmp/code on line 5&lt;/LI-CODE&gt;&lt;/LI&gt;
&lt;LI&gt;
&lt;P&gt;The expected outcome (and maybe this can be achieved by going a different path...like not using custom code ?)&amp;nbsp; is that, while iterating through the file, i am getting the following output:&lt;/P&gt;
&lt;P&gt;1st iteration:&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;vMod = 03_TRANSFORM/03_REVENUE/MOD_Table_1.qvs&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;vStore =&amp;nbsp;call StoreIntoSDL ('MOD', 'Table_1', '03_REVENUE', 'Table_1');&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;2nd iteration&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;vMod = 03_TRANSFORM/03_REVENUE/MOD_Table_2.qvs&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;vStore =&amp;nbsp;call StoreIntoSDL ('MOD', 'Table_2', '03_REVENUE', 'Table_2');&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;and so on...&amp;nbsp;&lt;/P&gt;
&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have out together a sample automation, just to get the idea. (For some reason, it runs into an error, but you should get the idea of what i am after)&lt;/P&gt;
&lt;P&gt;I appreciate every hint and help!&amp;nbsp; Thanks in advance!&lt;/P&gt;</description>
      <pubDate>Wed, 13 Sep 2023 05:11:31 GMT</pubDate>
      <guid>https://community.qlik.com/t5/Qlik-Automate/Follow-Up-Question-Parse-Text-Custom-Code/m-p/2116123#M2452</guid>
      <dc:creator>xyz_1011</dc:creator>
      <dc:date>2023-09-13T05:11:31Z</dc:date>
    </item>
    <item>
      <title>Re: Follow Up Question: Parse Text / Custom Code</title>
      <link>https://community.qlik.com/t5/Qlik-Automate/Follow-Up-Question-Parse-Text-Custom-Code/m-p/2116349#M2457</link>
      <description>&lt;P&gt;I got it to work mysel using the following custom code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="php"&gt;$gen = $inputs['gen'];
// Extract include statements
// Initialize arrays to store include statements and StoreIntoSDL lines
$include_statements = array();
$store_into_sdl = array();

// Extract include statements and StoreIntoSDL lines
preg_match_all('/Must_Include=\$\(vModulesRoot\)([^;]+);/', $gen, $include_matches);
preg_match_all('/call\s+StoreIntoSDL\s+\((.*?)\);/', $gen, $sdl_matches);

// Process each match
foreach ($include_matches[1] as $include_match) {
    $include_statements[] = trim($include_match);
}

foreach ($sdl_matches[1] as $sdl_match) {
    $store_into_sdl[] = trim($sdl_match);
}

// Create an array to store the results
$json_array = array();

// Combine include statements and StoreIntoSDL lines into a JSON object
for ($i = 0; $i &amp;lt; count($include_statements); $i++) {
    $include_statement = str_replace('\\', '/', $include_statements[$i]);
    $store_into_sdl_line = "call StoreIntoSDL ({$store_into_sdl[$i]});";
    
    $json_array[] = array(
        'include' =&amp;gt; $include_statement,
        'store_into_sdl' =&amp;gt; $store_into_sdl_line
    );
}

// Convert JSON object to JSON string
echo json_encode($json_array);&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 13 Sep 2023 07:34:54 GMT</pubDate>
      <guid>https://community.qlik.com/t5/Qlik-Automate/Follow-Up-Question-Parse-Text-Custom-Code/m-p/2116349#M2457</guid>
      <dc:creator>xyz_1011</dc:creator>
      <dc:date>2023-09-13T07:34:54Z</dc:date>
    </item>
  </channel>
</rss>

