This source code below was created in 2008 and it is still being used today, but there is now a Trading Blox function: GetFieldCount()

This new function performs a similar count.

 

This custom function Field_Count returns the number of characters in the tested string that are a comma.  Each comma found increments the field count.  This source ensures that when no commas are in a text string, but the character count of the string is greater than zero, the count will be one.  

 

If the string is empty and has a length equal to zero, or only contains the comma, the result returned will be zero.  If there is one field and one comma, the return is one.  Two fields and two commas returns two, etc.

Example:

'  ==============================================================
'  FUNCTION NAME: Field_Count
'  ==============================================================
'   DESCRIPTION:  This function returns the number of comma
'                 delimited fields in the passed string variable.
'   USE:
'      When the count of the number of comma separated values is
'      needed required for the GetField function used.  
'   CODE FUNCTION CALL:
'     Function_Result = Script.Execute( "Field_Count", _
'                                        sAnyStringFieldGroup )
'        OR
'     PRINT Script.Execute( "Field_Count", sAnyStringFieldGroup )
'  --------------------------------------------------------------
'     FUNCTION START - Field_Count
'  --------------------------------------------------------------
'  Function Parameter Variables
VARIABLES: String_Fields_fc                 Type: String
'  Function Working Variables
VARIABLES: BeginPtr_fc, Field_Count_fc       Type: Integer
VARIABLES: EndPtr_fc, String_Length_fc       Type: Integer
'  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'  Assign ParameterList Items to Parameter Variables
'  so that Code is Self-Documenting, and so users can
'  easily see parameter requirements.
String_Fields_fc = script.stringParameterList[1] '  STRING
'  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'  Determine the Size of the String
String_Length_fc = Len(String_Fields_fc)

 
'  When there is enough Characters,...
If String_Length_fc > 0 THEN
 
  BeginPtr_fc = 1     '  Start at the first character
  Field_Count_fc = 1   '  No Delimiters Found Indicate only 1 Field
 
  ' Look at each character in the string
  For EndPtr_fc = 1 TO String_Length_fc
 
    '  If the Character is a Delimiter,...
    If FindString( mid(String_Fields_fc, EndPtr_fc, 1), "," ) > -1 THEN
        ' Count the field
        Field_Count_fc = Field_Count_fc + 1
       
        '  Adjust the Pointer's Character Location
        BeginPtr_fc = EndPtr_fc + 1
    ENDIF
  Next
ELSE  
  '  Empty String Has No Fields
  Field_Count_fc = 0
ENDIF      
'  Field_Count_fc Shows the number of Fields found
script.SetReturnValue( Field_Count_fc )   '  Return Function Value
'  --------------------------------------------------------------
'  Field_Count -  FUNCTION END
'  ==============================================================

 


Edit Time: 10/29/2020 10:52:28 AM


Topic ID#: 242

Created with Help & Manual 7 and styled with Premium Pack Version 2.80 © by EC Software