Clarity Service Management

Expand all | Collapse all

TIP SPEL: String of values to array (spell_object)

Jump to Best Answer
  • 1.  TIP SPEL: String of values to array (spell_object)

    Posted 09-13-2019 03:00 PM
    Edited by Daniel Becker Bighelini 28 days ago
    Hi Guys,

    TIP SPEL: How to transform a string of values ​​into an array without using the split function?
    
    Just use the spell_object method as shown below.
    My first post here... :)
    Enjoy.

    void zSplitString(...)
    {
    	////////////////////////////////////////////////////////////////////////
    	// Method:			zSplitString
    	// Author:			Daniel Becker Bighelini
    	// Created:			09/13/2019
    	//
    	// Description:
    	// - Receives a string of values ​​separated by a delimiter and return an
    	//   array of processed values;
    	// - If an object is entered in the call, there will be a pointer 'this'
    	//   and the object can be manipulated if necessary;
    	// - This function can only be invoked via the 'spell_object' method;
    	// - The function 'zSplitStringSample' was created to exemplify the
    	//   use this function.
    	//
    	// Arguments:
    	// arg[0]: (string) zs_str		Input string to split in 'msg' array
    	// arg[1]: (string) zs_delim	String delimiter to tokens
    	// arg[2]: (string) zs_regex	Regex will be used to identify tokens
    	// arg[3]: (int) zi_debug		[OPCIONAL] Define debug mode
    	//
    	// Example:
    	// zSplitStringSample()
    	//
    	// Returns:
    	// msg[0]: 'NOME'
    	// msg[1]: 'ENDEREÇO'
    	// msg[2]: 'TELEFONE'
    	//
    	////////////////////////////////////////////////////////////////////////
    	// Variaveis globais do metodo
    	int zi_debug;
    	string zs_method, zs_str, zs_value, zs_delim, zs_regex;
    	
    	// Atribuindo variaveis
    	zs_method = is_empty(class()) ? 'zSplitString' : format("%s zSplitString", class());
    	zs_str 						= (string) argv[0];
    	zs_delim					= (string) argv[1];
    	zs_regex					= (string) argv[2];
    	if (argc>3) zi_debug		= (int) argv[3];
    	if (is_empty(zs_str)) zs_str = '';
    	if (is_empty(zs_delim)) zs_delim = ',';
    	if (is_empty(zs_regex)) zs_regex = '[A-Z]+';
    	
    	if (zi_debug > 0) {
    		if (!is_empty(event())) logf(SIGNIFICANT, "%s EVENTO     : '%s'", zs_method, event());
    		logf(SIGNIFICANT, "%s ENTRADA    : '%s'", zs_method, zs_str);
    		logf(SIGNIFICANT, "%s DELIMITADOR: '%s'", zs_method, zs_delim);
    		logf(SIGNIFICANT, "%s REGEX      : '%s'", zs_method, zs_regex);
    		if (!is_empty(this)) logf(SIGNIFICANT, "%s OBJETO     : '%s'", zs_method, (string) this);
    	}
    	
    	///////////////////////////////////////////////////////////////////////////////////////
    	// Inicio do codigo
    
    	int zi_idx, zi_idx_start, zi_idx_end;
    	zi_idx = 0;
    	zi_idx_start = sindex(zs_str, zs_regex, 0);
    	zi_idx_end = sindex(zs_str, zs_delim, zi_idx_start+1);
    	if (zi_idx_end == -1) zi_idx_end = strlen(zs_str);
    
    	// Retornando valores
    	if (zi_debug > 0) logf(SIGNIFICANT, "%s Retornando valores...", zs_method);
    	do {
    		zs_value = substr(zs_str, zi_idx_start, zi_idx_end-zi_idx_start);
    		if (zi_debug > 0) logf(SIGNIFICANT, "%s msg[%d]     : '%s'", zs_method, zi_idx, zs_value);
    		set_return_data(zi_idx++, zs_value);
    
    		zi_idx_start = sindex(zs_str, zs_regex, zi_idx_end+1);
    		zi_idx_end = sindex(zs_str, zs_delim, zi_idx_start+1);
    		if (zi_idx_end == -1) zi_idx_end = strlen(zs_str);
    	} while (zi_idx_start > -1);
    	
    	if (zi_debug > 0) logf(SIGNIFICANT, "%s SAIDA      : Array de %d posicoes", zs_method, zi_idx);
    	
    	// Fim do codigo
    	///////////////////////////////////	
    }
    
    
    void zSplitStringSample()
    {
    	////////////////////////////////////////////////////////////////////////
    	// Method:			zSplitStringSample
    	// Author:			Daniel Becker Bighelini
    	// Created:			09/13/2019
    	//
    	// Description:		Call example function 'zSplitString'.
    	//
    	// Example:			zSplitStringSample()
    	//
    	// Returns:
    	// msg[0]: 'NOME'
    	// msg[1]: 'ENDEREÇO'
    	// msg[2]: 'TELEFONE'
    	////////////////////////////////////////////////////////////////////////
    	int zi_i, zi_debug;
    	string zs_method;
    	
    	// Atribuindo variaveis
    	zs_method = 'zSplitStringSample';
    	zi_debug = 1;						// Debug mode
    	
    	///////////////////////////////////////////////////////////////////////////////////////
    	// Inicio do codigo
    
    	// Informacoes da string
    	string zs_str, zs_delim, zs_regex;
    	zs_str = 'NOME;ENDEREÇO;TELEFONE';	// Sample string data
    	zs_delim = ';';
    	zs_regex = '[aA0-zZ9]+';
    
    	// Executando metodo interpretativo
    	if (zi_debug > 0) logf(SIGNIFICANT, "%s Encaminhando string de valores ao metodo 'zSplitString'...", zs_method);
    	send_wait(0, spell_object(),		// @|bop_cmd-#8184|doit|0|0
    				"zSplitString",			// interpretative method name
    				(string) zs_method,		// class name (same as class())
    				(string)'PARSER',		// event name (same as event())
    				(object) NULL,			// "this" pointer of type object
    				(string) zs_str,		// String to split
    				(string) zs_delim,		// String delimiter
    				(string) zs_regex,		// Regex parser
    				(int) zi_debug			// [OPTIONAL] Debug mode
    				);
    
    	// Recebendo valores
    	if (zi_debug > 0) logf(SIGNIFICANT, "%s Valores recebidos: %d", zs_method, msg_length());
    	for (zi_i=0;zi_i<msg_length();zi_i++) {
    		logf(SIGNIFICANT, "%s #%d: '%s'\n", zs_method, zi_i, (string) msg[zi_i]);
    	}
    
    	// Fim do codigo
    	///////////////////////////////////	
    }


    ------------------------------
    CA Service Desk Manager Especialist Developer
    https://www.facebook.com/groups/usuariossdmbrasil/
    ------------------------------


  • 2.  RE: TIP SPEL: String of values to array (spell_object)

    Posted 30 days ago
    Thank you for sharing this with the community Daniel!

    ------------------------------
    Chris Hackett
    Community Manager, Broadcom Enterprise Software Division
    Broadcom Inc.
    ------------------------------



  • 3.  RE: TIP SPEL: String of values to array (spell_object)

     
    Posted 29 days ago
    Thanks Daniel,

    Great to see a general purpose "utility" SPL code contribution here to the Communities. 

    These are a great base on which to build, and are often more useful to others than a specific purpose piece of code. (Even if I've got no idea what it does!)

    Maybe you could give an example use case of what someone might do with this, or why you built it?

    You might also want to add in an English translation of the Portuguese in the readme notes.  I've used Google Translate to whip up this version, but I've no idea if it is accurate!


    // Receives a string of values ​​separated by a delimiter and
    // return an array of processed values;
    // - If an object is entered in the call, there will be a pointer 'this'
    // and the object can be manipulated if necessary;
    // - This function can only be invoked via the 'spell_object' method;
    // - The function 'zSplitStringSample' was created to exemplify the
    // use this function.
    //

    Thanks for providing the code!

    Kyle_R


  • 4.  RE: TIP SPEL: String of values to array (spell_object)
    Best Answer

    Posted 28 days ago
    Thanks Kyle,
    Doc updated!

    ------------------------------
    CA Service Desk Manager Especialist Developer
    PROCERGS
    ------------------------------