ArrayTools

SwordfishUser.ArrayTools History

Hide minor edits - Show changes to output

Added lines 254-333:
=]

!!!A demostration program showing the results on a terminal.
=code [=

{
*****************************************************************************
*  Name    : Test_FIND_STRING.bas                                          *
*  Author  : Mark Rodgers                                                  *
*  Notice  : Copyright (c) 2007 Mark Rodgers                                *
*          : All Rights Reserved                                            *
*  Date    : 19/09/2007                                                    *
*  Version : 1.0                                                            *
*  Notes  :                                                                *
*          :                                                                *
*****************************************************************************
}
'******************************************************************************

'******************************************************************************
Program Test_FIND_STRING

'******************************************************************************

'******************************************************************************
    Include "usart.bas"                            'ONLY USED IN DEBUG OUTPUT
    Include "convert.bas"                          'ONLY USED IN DEBUG OUTPUT
    Include "Array_Tools.bas"                      'contains "FIND_STRING" function

'******************************************************************************

'******************************************************************************
    Dim test_array(100) As Char                    'our test array
    Dim temp_index As Byte                        'used in array initialization
    Dim next_character_position As Byte            'the position in the array immediately after the last character in our search string

'******************************************************************************

'******************************************************************************
'INITIALISE THE ARRAY, STARTING AT
'1(DECIMAL).
'##################################
temp_index=0
While temp_index<Bound(test_array)
    test_array(temp_index)=temp_index+1
    Inc(temp_index)
Wend
'##################################



'SEARCH FOR A STRING OF CHARACTERS
'IN AN ARRAY AND RETURN THE POSITION
'OF THE CHARACTER IMMEDIATELY AFTER
'THE LAST CHARACTER IN THE SEARCH STRING.
'
'THE DATA PRODUCED IN THE TERMINAL:-
'
'Start Position=0
'string_to_find=12345
'String Length=5
'start character found=1
'start position=48
'compare string created=12345
'next_character_position=53
'next_character=6
'##################################
While 1=1
  next_character_position=FIND_STRING("12345",test_array,0,Bound(test_array))
 
  USART.SetBaudrate(br9600)'DEBUG OUTPUT
  USART.Write("next_character_position=",DecToStr(next_character_position),13)'DEBUG OUTPUT
  USART.Write("next_character=",test_array(next_character_position),13,13)'DEBUG OUTPUT
    DelayMS(1000)
Wend
'##################################

'******************************************************************************

'******************************************************************************
Deleted line 200:
  
Deleted lines 253-260:

'******************************************************************************

'******************************************************************************
   
'******************************************************************************

'******************************************************************************
Deleted line 71:
   'Dim next_character_position as byte            'the position in the array immediately after the last character in our search string
Added lines 73-75:
   'position in array immediately after last character in our search string
    'Dim next_character_position as byte
    '
Deleted lines 151-158:
   result=0                                        'when returned a value of zero indicates the string was not found(any good string must have more than zero characters"


USART.SetBaudrate(br9600)
USART.Write("Start Position=",DecToStr(loop),13)'DEBUG OUTPUT
USART.Write("string_to_find=",string_to_find,13)'DEBUG OUTPUT
USART.Write("String Length=",DecToStr(string_to_find_length),13)'DEBUG OUTPUT

Added lines 153-163:
   'when returned a value of zero indicates the string was not found
    '(any good string must have more than zero characters)
    result=0                                       


USART.SetBaudrate(br9600)
USART.Write("Start Position=",DecToStr(loop),13)'DEBUG OUTPUT
USART.Write("string_to_find=",string_to_find,13)'DEBUG OUTPUT
USART.Write("String Length=",DecToStr(string_to_find_length),13)'DEBUG OUTPUT

   
Changed lines 167-168 from:
   If string_to_find_length<2 Then                'there must be at least 2 characters in the string
to:
   'there must be at least 2 characters in the string
    If string_to_find_length<2 Then              
Changed lines 179-180 from:
   If start_of_file<end_of_file Then             'the start of file must be less than the end of file
        If end_of_file<start_of_file+string_to_find_length Then'the start point must allow the full string length to be read
to:
   'the start of file must be less than the end of file
    If start_of_file<end_of_file Then             
       
        'the start point must allow the full string length to be read
        If end_of_file<start_of_file+string_to_find_length Then
Deleted lines 200-203:
   While temp_char<>char_to_find And loop<end_of_file'look for the position in the array of the occurence of the character to find
        temp_char=array_to_test(loop)              'test this position in the array
        Inc(loop)                                  'move to next position
    Wend
Changed lines 202-210 from:
   If temp_char=array_to_test(loop-1) Then        'test validity of data, loop is currently at required start character plus 1 due to "Inc(loop)" in while-wend routine above   
to:
   'look for the position in the array of the occurence of the character to find
   While temp_char<>char_to_find And loop<end_of_file
        temp_char=array_
to_test(loop)               'test this position in the array
        Inc(loop)                                  'move to next position
    Wend
   
    'test validity of data, loop is currently at required start character
    'plus 1 due To "Inc(loop)" in While-Wend routine above
    If temp_char=array_to_test(loop-1) Then       
   
Changed lines 214-216 from:
       If loop<end_of_file-string_to_find_length Then'only test if there are enough characters left
to:
      
        'only test if there are enough characters left
        If loop<end_of_file-string_to_find_length Then
Changed lines 219-221 from:
           While loop<temp_loop+string_to_find_length'check a maximum of "string_to_find_length" characters
to:
          
   
       'check a maximum of "string_to_find_length" characters
            While loop<temp_loop+string_to_find_length
Changed lines 229-230 from:
           If temp_string=string_to_find Then      'the strings are equal and the index can now be found
   
            result=loop                        'the returned value is the index in the array of the character immediately following the last one in the "string_to_find"
to:
          
 
         'the strings are equal and the index can now be found
            If temp_string=string_to_find Then     
               
               'the returned value is the index in the array of the character
                'immediately following the last one in the
"string_to_find"
                result=loop                     
Changed lines 239-241 from:
               GoTo find_start                   'the "string_to_find was not found so keep checking until the end of the file
to:
              
 
              'the "string_to_find was not found so keep checking until the end of the file
                GoTo find_start                   
Added lines 255-262:

'******************************************************************************

'******************************************************************************
   
'******************************************************************************

'******************************************************************************
Changed lines 12-231 from:
I hope to add tools as I develop them in the near future.
to:
The module contains DEBUG lines that send information via the Hardware Serial port, these can all be removed or commented out if not required, the DEBUG lines are very clearly marked with the comment "'DEBUG OUTPUT", none of these lines are required to make the module work.

I hope to add tools as I develop them in the near future.

!!!The Array Manipulation Module Code

=code [=

{
*****************************************************************************
*  Name    : Array_Tools.bas                                                *
*  Author  : Mark Rodgers                                                  *
*  Notice  : Copyright (c) 2007 Mark Rodgers                                *
*          : All Rights Reserved                                            *
*  Date    : 19/09/2007                                                    *
*  Version : 1.0                                                            *
*  Notes  :                                                                *
*          :                                                                *
*****************************************************************************
}
'******************************************************************************

'******************************************************************************
    Module Array_Tools
   
'******************************************************************************

'******************************************************************************
    Include "string.bas"
    Include "usart.bas"'only required for DEBUG OUTPUT
    Include "convert.bas"'only required for DEBUG OUTPUT
   
'******************************************************************************

'******************************************************************************

'HOW "Public Function FIND_STRING" IS USED:-
'##############################################################################
    'FIND_STRING(string_to_find As String,ByRef array_to_test() As Char,start_of_file As Word,end_of_file As Word) As Word
    '"string_to_find" IS THE STRING YOU ARE LOOKING FOR IN YOUR DATA ARRAY,
    'CURRENTLY THE MAXIMUM LENGTH IS AS THE COMPILERS DEFAULT OF 23 CHARACTERS.
    '
    '"array_to_test()" IS THE ARRAY OF DATA YOU HAVE CREATED, NO RESTRICTION IS
    'PLACED ON THE MAXIMUM SIZE ABOVE THOSE OF THE COMPILER.
    '
    '"start_of_file" IS THE STARTING POINT IN THE ARRAY THAT YOU WISH TO BEGIN
    'THE SEARCH, MUST BE NO MORE THAN "end_of_file" MINUS "string_to_find_length".
    '
    '"end_of_file" IS THE LENGTH OF THE FILE WITHIN THE ARRAY, UP TO THE FULL
    'SIZE OF THE ARRAY, THIS IS NOT AN EXPLCIT VALUE AS THE START MUST BE, BUT
    'IS THE VALUE OF THE LENGTH OF THE KNOWN FILE INSIDE THE ARRAY.
    '
    'EXAMPLE:-
    '##################################
    'Include "usart.bas"
    'Include "convert.bas"
    'include "Array_Tools.bas"
    '
    'Dim test_array(100) as Char                    'our test array
    'Dim temp_index as byte                        'used in array initialization
    'Dim next_character_position as byte            'the position in the array immediately after the last character in our search string
    '
    ''INITIALISE THE ARRAY, STARTING AT 1
    '##################################
    'temp_index=0
    'While temp_index<Bound(test_array)
        'test_array(temp_index)=temp_index+1
        'Inc(temp_index)
    'Wend
    '##################################
    '
    'While1=1
    '  next_character_position=FIND_STRING("12345",test_array,0,Bound(test_array))
    ' 
    '  USART.SetBaudrate(br9600)'DEBUG OUTPUT
    '  USART.Write("next_character_position=",DecToStr(next_character_position),13)'DEBUG OUTPUT
    '  USART.Write("next_character=",test_array(next_character_position),13,13)'DEBUG OUTPUT
    ' 
    '  DelayMS(1000)
    'Wend
    '##################################
    '
    'THIS PROGRAM PRODUCES THIS OUTPUT ON A TERMINAL:-
    '
    'Start Position=0
    'string_to_find=12345
    'String Length=5
    'start character found=1
    'start position=48
    'compare string created=12345
    'next_character_position=53
    'next_character=6
    '
    'THE POSITION FOR THE FIRST CHARACTER MAY SEEM STRANGE BUT AS THE ARRAY IS
    'OF "Char" TYPE IS WILL LOOK AT THE ASCII VALUES PRINTED ON A TERMONAL SCREEN
    'AND NOT THEIR DECIMAL VALUE, THEREFORE 1 ("1") IS DECIMAL 49 AND IS LOCATED
    'AT POSITION 48 IN THE ARRAY.
'##############################################################################


'HOW "Public Function FIND_STRING" WORKS:-
'##############################################################################
    'USE THE "char_to_find" TO SEARCH THE ARRAY FOR THE FIRST OCCURENCE
    'OF THIS REFERENCE CHARACTER, ONCE FOUND A "temp_string" IS GENERATED
    'OF "string_to_find_length", THAT IS COMPARED WITH "string_to_find",
    'IF THEY ARE EQUAL THE VALUE OF "loop" IS GIVEN TO "result" AND RETURNED
    'TO THE CALLING ROUTINE.
    '
    'IF THE "temp_string" IS NOT EQUAL TO "string_to_find" THE "char_to_find"
    'IS SEARCHED FOR AGAIN, AND IF FOUND RE-TESTED AS ABOVE.
    '
    'IF NOTHING IS FOUND THAT COMPARES TO "string_to_find", THE "result"
    'IS GIVEN A VALUE OF 0 TO INDICATE THE ROUTINE FAILED.
    '
    'THE ROUTINE WILL FAIL IF THE "string_to_find" DOES NOT HAVE 2 OR MORE
    'CHARACTERS, "result" WILL BE 0 EVEN IF "string_to_find" HAS ONE CHARACTER.
    '
    'THE ROUTINE WILL ALSO FAIL IF THE START ADDRESS IS LARGER THAN THE END
    'ADDRESS, OR IF THERE IS NOT ENOUGH ROOM FROM THE START ADDRESS TO READ THE
    'REQUIRED CHARACTERS IN "string_to_find".
'##############################################################################

Public Function FIND_STRING(string_to_find As String,ByRef array_to_test() As Char,start_of_file As Word,end_of_file As Word) As Word
    Dim loop As Word                                'print loop counter
    Dim array_loop As Word                          'array find loop counter
    Dim temp_index As Word                          'index for "temp_string"
    Dim temp_loop As Word                          'temporary loop value
    Dim temp_string As String                      'holds the string to compare to the one we wish to find
    Dim char_to_find As Char                        'the character to look for
    Dim temp_char As Char                          'temporary character storage
    Dim string_to_find_length As Byte              'holds the length of the string
   
    loop=start_of_file                              'start at "start_of_file" position
    array_loop=0                                    'start at zero
    temp_loop=0                                    'initialize
    char_to_find=string_to_find(0)                  'initial character to find
    temp_char=255                                  'initialise
    string_to_find_length=Length(string_to_find)    'the length of the string we are looking for
    result=0                                        'when returned a value of zero indicates the string was not found(any good string must have more than zero characters"


USART.SetBaudrate(br9600)
USART.Write("Start Position=",DecToStr(loop),13)'DEBUG OUTPUT
USART.Write("string_to_find=",string_to_find,13)'DEBUG OUTPUT
USART.Write("String Length=",DecToStr(string_to_find_length),13)'DEBUG OUTPUT

   
    'THE ROUTINE MUST HAVE AT LEAST 2
    'CHARACTERS, IF NOT "result"=0.
    '##################################
    If string_to_find_length<2 Then                'there must be at least 2 characters in the string
USART.Write("NOT ENOUGH CHARACTERS",13)'DEBUG OUTPUT
        result=0                                    'indicate the routine has failed
        Exit                                        'exit the routine
    EndIf
    '##################################
   
   
    'TEST FOR CORRECT START AND END
    'OF FILE VALUES.
    '##################################
    If start_of_file<end_of_file Then              'the start of file must be less than the end of file
        If end_of_file<start_of_file+string_to_find_length Then'the start point must allow the full string length to be read
USART.Write("START VALUE TOO HIGH, NO ROOM FOR READ",13)'DEBUG OUTPUT           
            result=0                                'indicate the routine has failed           
            Exit                                    'exit the routine
        EndIf
       
        Else                                        'start of file is greater than end of file
USART.Write("START VALUE HIGHER THAN END VALUE",13)'DEBUG OUTPUT
        result=0                                    'indicate the routine has failed
        Exit                                        'exit the routine
       
    EndIf
    '##################################
   
   
    'BEGIN THE SEARCH.
    '##################################
find_start:   
    While temp_char<>char_to_find And loop<end_of_file'look for the position in the array of the occurence of the character to find
        temp_char=array_to_test(loop)              'test this position in the array
        Inc(loop)                                  'move to next position
    Wend
   
    If temp_char=array_to_test(loop-1) Then        'test validity of data, loop is currently at required start character plus 1 due to "Inc(loop)" in while-wend routine above   
        Dec(loop)                                  'move back one position
USART.Write("start character found=",array_to_test(loop),13)'DEBUG OUTPUT
USART.Write("start position=",DecToStr(loop),13)'DEBUG OUTPUT           
        If loop<end_of_file-string_to_find_length Then'only test if there are enough characters left
            temp_index=0                            'ensure this counter starts at zero
            temp_loop=loop                          'remember current position
            While loop<temp_loop+string_to_find_length'check a maximum of "string_to_find_length" characters
                temp_string(temp_index)=array_to_test(loop)'create the temporary string
                Inc(loop)                          'look at the next charater
                Inc(temp_index)                    'next character in "temp_string"   
            Wend
           
USART.Write("compare string created=",temp_string,13)'DEBUG OUTPUT
DelayMS (100)'DEBUG OUTPUT   
            If temp_string=string_to_find Then      'the strings are equal and the index can now be found
                result=loop                        'the returned value is the index in the array of the character immediately following the last one in the "string_to_find"
   
                Else
USART.Write("string not found",13)'DEBUG OUTPUT       
                GoTo find_start                    'the "string_to_find was not found so keep checking until the end of the file
            EndIf
        EndIf
       
        Else
USART.Write("next start character not found",13)'DEBUG OUTPUT
    EndIf
    '##################################
   
End Function

'******************************************************************************

'******************************************************************************
=]
Added lines 1-12:
!! Array manipulation Tools by Mark Rodgers

!!!Module Notes
The code is fully commented and is written to allow me to understand how to work with arrays of data.

The first tool is designed to use a known string of data and search an array for its occurence and return the position in the array of the character immediately following the last character of the search string.

I am using this to parse an array of data from a Telit GE863-GPS MODEM in order to find the position of the NMEA data following the echoed command sent to the MODEM.

All details of use and operation and enclosed along with a working example and the results obtained with real hardware!

I hope to add tools as I develop them in the near future.