AMX SPLIT_STRING
Frequently Asked Questions
Brand:
- AMX
Models:
- Netlinx
Question:
Is there a way to assign sub-strings separated by delimiters from a string to an array?
Answer:
See SPLIT_STRING.axi located in the download:
PROGRAM_NAME='SPLIT_STRING'
(***********************************************************)
(***********************************************************)
(* FILE_LAST_MODIFIED_ON: 04/05/2006 AT: 09:00:25 *)
(***********************************************************)
(* System Type : NetLinx *)
(***********************************************************)
(* REV HISTORY: *)
(***********************************************************)
(*
$History: $
*)
(***********************************************************)
(* DEVICE NUMBER DEFINITIONS GO BELOW *)
(***********************************************************)
DEFINE_DEVICE
(***********************************************************)
(* CONSTANT DEFINITIONS GO BELOW *)
(***********************************************************)
DEFINE_CONSTANT
(***********************************************************)
(* DATA TYPE DEFINITIONS GO BELOW *)
(***********************************************************)
DEFINE_TYPE
(***********************************************************)
(* VARIABLE DEFINITIONS GO BELOW *)
(***********************************************************)
DEFINE_VARIABLE
(***********************************************************)
(* LATCHING DEFINITIONS GO BELOW *)
(***********************************************************)
DEFINE_LATCHING
(***********************************************************)
(* MUTUALLY EXCLUSIVE DEFINITIONS GO BELOW *)
(***********************************************************)
DEFINE_MUTUALLY_EXCLUSIVE
(***********************************************************)
(* SUBROUTINE/FUNCTION DEFINITIONS GO BELOW *)
(***********************************************************)
(* EXAMPLE: DEFINE_FUNCTION <RETURN_TYPE> <NAME> (<PARAMETERS>) *)
(* EXAMPLE: DEFINE_CALL '<NAME>' (<PARAMETERS>) *)
DEFINE_FUNCTION SPLIT_STRING (CHAR strINPUT[], CHAR strDELIMITER[], CHAR arOUTPUT[][])
(********************************************************************************)
(* SPLIT_STRING FUNCTION *)
(* Populates an array with the sub-strings from a delimited string *)
(* The syntax: *)
(* *)
(* SPLIT_STRING (CHAR strINPUT[], CHAR strDELIMITER[], CHAR arOUTPUT[][]) *)
(* *)
(* PARAMETERS: *)
(* strINPUT = string variable containing delimited data *)
(* strDELIMITER = value of the delimiter *)
(* arOUTPUT = the array that will be populated *)
(* *)
(* RESULT: *)
(* arOUTPUT elements will contain the sub-strings *)
(* *)
(* EXAMPLES: *)
(* strVAR = 'this,is,a,string,with,comma,delimiters' *)
(* SPLIT_STRING(strVAR,',',arRESULT) *)
(* RESULT: *)
(* arRESULT[1] is 'this' *)
(* arRESULT[2] is 'is' *)
(* arRESULT[3] is 'a' *)
(* arRESULT[4] is 'string' *)
(* arRESULT[5] is 'with' *)
(* arRESULT[6] is 'comma' *)
(* arRESULT[7] is 'delimiters' *)
(* *)
(* NOTE: *)
(* arRESULT must be defined in DEFINE_VARIABLE as a multidimensional array *)
(* specifying the maximum number of elements, and maximum element length *)
(* *)
(* DEFINE_VARIABLE *)
(* CHAR arRESULT[50][50] *)
(* *)
(********************************************************************************)
STACK_VAR
CHAR strTEMP[1000]
CHAR strTRASH[100]
INTEGER nLOOP
INTEGER lenDELIMITER
{
strTEMP = strINPUT //use a temporary string to keep integrity of original string
nLOOP = 1 //variable used to track how many sub-strings there are and how many elements should be used
lenDELIMITER = LENGTH_STRING(strDELIMITER) //length of delimiter
WHILE(LENGTH_STRING(strTEMP)) //keep looking until the end of the string
{
IF(FIND_STRING(strTEMP,strDELIMITER,1)) //look for the delimiter, if it is in the string proceed
{
arOUTPUT[nLOOP]=REMOVE_STRING(strTEMP,strDELIMITER,1) //assign sub-strings to array element
IF(RIGHT_STRING(arOUTPUT[nLOOP],lenDELIMITER)=strDELIMITER)
{
SET_LENGTH_STRING(arOUTPUT[nLOOP],LENGTH_STRING(arOUTPUT[nLOOP])-lenDELIMITER) //remove delimiter from array element
}
}
ELSE //no more delimiters
{
arOUTPUT[nLOOP]=strTEMP //assign remaining portion of string to array element
SET_LENGTH_STRING(strTEMP,0) //clear temporary string to kill loop
}
SET_LENGTH_ARRAY(arOUTPUT,nLOOP) //set length of array
nLOOP++ //increment
}
}
(***********************************************************)
(* STARTUP CODE GOES BELOW *)
(***********************************************************)
DEFINE_START
(***********************************************************)
(* THE EVENTS GO BELOW *)
(***********************************************************)
DEFINE_EVENT
(*****************************************************************)
(* *)
(* !!!! WARNING !!!! *)
(* *)
(* Due to differences in the underlying architecture of the *)
(* X-Series masters, changing variables in the DEFINE_PROGRAM *)
(* section of code can negatively impact program performance. *)
(* *)
(* See Ԅifferences in DEFINE_PROGRAM Program ExecutionԠsection *)
(* of the NX-Series Controllers WebConsole & Programming Guide *)
(* for additional and alternate coding methodologies. *)
(*****************************************************************)
DEFINE_PROGRAM
(*****************************************************************)
(* END OF PROGRAM *)
(* *)
(* !!! DO NOT PUT ANY CODE BELOW THIS COMMENT !!! *)
(* *)
(*****************************************************************)