Sign in to follow this  
Followers 0
Rofellos

SplitRange( $sRange )

3 posts in this topic

#1 ·  Posted (edited)

http://ratsoftware.blogspot.com

A very useful function when working with data ranges would get a string in the format "1 - 5 ; 7 ; 9 ; 14 - 26" and convert that data into a 2D array with these values. The function presented adds to each row the initial and the final values of each range. When the range is composed of a single point, the initial and final values are equal at this point.

#comments-start
/* **********************************************************************
*
*  $FC Function: SplitRange
*
*  $ED Function Description
*       Returns a 2D array of ranges. Each row contain the initial and final
*       value of the range. If the range is one single point, the initial 
*       and final values are the same.
*
*  $EP Parameters
*       - $sRange: String that contains the range  .
*
*  $FV Returned Values
*       - $aRangeArray: A 2D array of initial and final value of a range.
*       - 0: If failure
*       
*
********************************************************************** */
#comments-end

Func SplitRange( $sRange )
    
    Local $iFlag = 0
    Local $iRangeFlag = 0
    Local $aStringArray = StringToArray( $sRange )
    Local $iStringArraySize = UBound( $aStringArray )
    Local $iCorrectCharracters = VerifyCharacters( $sRange )
    Local $iNumberOfRanges = ( CountCharacter( $sRange , ";" ) )
    Local $aRangeArray[$iNumberOfRanges][2]
    
    If ( $iCorrectCharracters =  1 ) Then
        For $i = 0 To ( $iStringArraySize - 1 )
            If ( Asc( $aStringArray[$i] ) >= 48 ) And ( Asc( $aStringArray[$i] ) <= 57 )  Then
                $aRangeArray[$iRangeFlag][$iFlag] &= $aStringArray[$i]              
            EndIf
        
            If ( $aStringArray[$i] = "-" ) Then
                $iFlag = 1
            ElseIf ( $aStringArray[$i] = ";" ) Then
                $iFlag = 0
                $iRangeFlag += 1
            EndIf
        Next
        
        For $j = 0 To ( UBound( $aRangeArray ) - 1 )
            If ( $aRangeArray[$j][1] = "" ) Then
                $aRangeArray[$j][1] = $aRangeArray[$j][0]
            EndIf
        Next
                
        Return $aRangeArray
    Else
        Return 0
    EndIf
        
EndFunc ; /* SplitRange */

; /* *************************** Funçoes de uso geral ************************* */


#comments-start
/* **********************************************************************
*
*  $FC Function: StringToArray
*
*  $ED Function Description
*       Converts a String into a character array.
*
*  $EP Parameters
*       - $sString: The String to be converted.
*
*  $FV Returned Values
*       $aCharArray - The array of characters
*
********************************************************************** */
#comments-end

Func StringToArray( $sString )
    
    Local $aASCIIArray = StringToASCIIArray( $sString )
    Local $iASCIIArraySize = UBound( $aASCIIArray )
    Local $aCharArray[ $iASCIIArraySize ]
    
    For $i = 0 To ( $iASCIIArraySize - 1 )
        $aCharArray[ $i ] =  Chr( $aASCIIArray[ $i ] )
    Next
    
    Return $aCharArray  
    
EndFunc ; /* StringToArray */ 

#comments-start
/* **********************************************************************
*
*  $FC Function: VerifyCharacters
*
*  $ED Function Description
*       Check if the input character array have only numbers, '-' and ';'.
*
*  $EP Parameters
*       - $aCharArray: The character array to be verified. 
*
*  $FV Returned Values
*       1 - If have only numbers, '-' and ';'.
*       0 - If not.
*
********************************************************************** */
#comments-end

Func VerifyCharacters( $sString )
    
    Local $aCharArray = StringToArray( $sString )
    Local $iCharArraySize = UBound( $aCharArray )
    
    For $i = 0 To ( $iCharArraySize - 1 )
        If ( $aCharArray[ $i ] <> " " ) And ( $aCharArray[ $i ] <> "-" ) And ( $aCharArray[ $i ] <> ";" ) Then
            If ( Asc( $aCharArray[ $i ] ) < 48 ) Or ( Asc( $aCharArray[ $i ] ) > 57 ) Then
                Return 0
            EndIf
        EndIf
    Next
    
    Return 1
    
EndFunc ; /* VerifyCharacters */ 

#comments-start
/* **********************************************************************
*
*  $FC Function: CountCharacter
*
*  $ED Function Description
*       Returns how many times a character appears in a string.
*
*  $EP Parameters
*       - $sString: The string to be verified.
*       - $cChar: The char to looking for.
*
*  $FV Returned Values
*       - $iCharCount: The number of occurence of the char into the string
*       
*
********************************************************************** */
#comments-end

Func CountCharacter( $sString , $cChar )
    
    Local $aCharArray = StringToArray( $sString )
    Local $iCharArraySize = UBound( $aCharArray )
    Local $iCharCount = 1
    
    For $i = 0 To ( $iCharArraySize - 1 )
        If ( $aCharArray[ $i ] = $cChar ) Then
            $iCharCount += 1 
        EndIf
    Next
    
    Return $iCharCount
    
EndFunc ; /* CountCharacter */
Edited by Rofellos

Share this post


Link to post
Share on other sites



Thanks JohnOne!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0