Función explode en PowerBuilder

En este post le mostrare una función que encontré por la web el cual es la función de explode si usas php te familiarizaras con esta función el cual tiene la función de dividir una cadena por el patrón a dividir en varias partes.
Creamos una función que se llame por ej fn_explode():


Return type: integer
Parámetros:
1. Pass by: value, Type: string, Name: as_source
2. Pass by: value, Type: string, Name: as_delimiter
3. Pass by: reference, Type: string, Name: as_array[]
long ll_DelLen, ll_Pos, ll_Count, ll_Start, ll_Length
string ls_holder

//Check for NULL
IF IsNull(as_source) or IsNull(as_delimiter) Then
  long ll_null
  SetNull(ll_null)
  Return ll_null
End If

//Check for at leat one entry
If Trim (as_source) = '' Then
  Return 0
End If

//Get the length of the delimeter
ll_DelLen = Len(as_Delimiter)
ll_Pos = Pos(Upper(as_source), Upper(as_Delimiter))

//Only one entry was found
if ll_Pos = 0 then
  as_Array[1] = as_source
  return 1
end if

//More than one entry was found - loop to get all of them
ll_Count = 0
ll_Start = 1

Do While ll_Pos > 0
  //Set current entry
  ll_Length = ll_Pos - ll_Start
  ls_holder = Mid (as_source, ll_start, ll_length)
  
  // Update array and counter
  ll_Count ++
  as_Array[ll_Count] = ls_holder
  
  //Set the new starting position
  ll_Start = ll_Pos + ll_DelLen
  ll_Pos = Pos(Upper(as_source), Upper(as_Delimiter), ll_Start)
Loop

//Set last entry
ls_holder = Mid (as_source, ll_start, Len (as_source))

// Update array and counter if necessary
if Len (ls_holder) > 0 then
  ll_count++
  as_Array[ll_Count] = ls_holder
end if

//Return the number of entries found
Return ll_Count


Ejemplo de uso:
string array_explode[]
// esta variable es de referencia, funciona como variable de instancia desde el objeto que declaras.
integer cant_explode, i
cant_explode = fn_explode('sourcepy-com', '-', array_explode[]);

for i = 1 to cant_explode
  messagebox('Valor', array_explode[i])
next

Comentarios

  1. I love your wordpress theme, exactly where did you down load it through?

    ResponderBorrar

Publicar un comentario

Entradas más populares de este blog

Obtener el anterior y siguiente registro de una tabla MySQL con PHP

Cargar dinamicamente un DropDownListBox con PowerBuilder