Skip to main content
Announcements
Accelerate Your Success: Fuel your data and AI journey with the right services, delivered by our experts. Learn More
cancel
Showing results for 
Search instead for 
Did you mean: 
xyz_1011
Partner - Creator
Partner - Creator

Follow Up Question: Parse Text / Custom Code

Hej all,

with regards to this post (thanks again @AfeefaTk ), 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).

To sum up, here is what i am after:

  1. I have a given file (.qvs) in my s3 bucket. The file can contain the following content:

    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;
    
     

    The important pieces are the includes and the StoreToSDL function. A given file can contain n occurances of includes and StoreToSDLs. 

  2. In the referred post the following custom code was suggested:

    $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 < count($include_statements); $i++) {
        $json_array[] = array(
            'include' => $include_statements[$i],
            'store_into_sdl' => $store_into_sdl[$i]
        );
    }
    
    // Convert JSON object to JSON string
    echo json_encode($json_array);​


    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:

    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
  3. The expected outcome (and maybe this can be achieved by going a different path...like not using custom code ?)  is that, while iterating through the file, i am getting the following output:

    1st iteration:

    vMod = 03_TRANSFORM/03_REVENUE/MOD_Table_1.qvs
    vStore = call StoreIntoSDL ('MOD', 'Table_1', '03_REVENUE', 'Table_1');

    2nd iteration

    vMod = 03_TRANSFORM/03_REVENUE/MOD_Table_2.qvs
    vStore = call StoreIntoSDL ('MOD', 'Table_2', '03_REVENUE', 'Table_2');

    and so on... 

 

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)

I appreciate every hint and help!  Thanks in advance!

Labels (2)
1 Solution

Accepted Solutions
xyz_1011
Partner - Creator
Partner - Creator
Author

I got it to work mysel using the following custom code:

 

$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 < 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' => $include_statement,
        'store_into_sdl' => $store_into_sdl_line
    );
}

// Convert JSON object to JSON string
echo json_encode($json_array);

View solution in original post

1 Reply
xyz_1011
Partner - Creator
Partner - Creator
Author

I got it to work mysel using the following custom code:

 

$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 < 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' => $include_statement,
        'store_into_sdl' => $store_into_sdl_line
    );
}

// Convert JSON object to JSON string
echo json_encode($json_array);