Sign in to follow this  
Followers 0
atwolf359

_StringSplitBydef

3 posts in this topic

This routine comes in handy in my my db work.

;routine parses 'fixed field, non-delimited' data strings.
#include <Array.au3>
$ary2 = _StringSplitBydef('0123456789abcdef', '4:3:0:2:1') ;$recdef as a field map
_ArrayDisplay($ary2, "StringSplitBydef   " & 'Record definition -- "4:3:0:2:1"')
$ary2 = _StringSplitBydef('0123456789abcdef', 3) ;$recdef as a fixed record spliter
_ArrayDisplay($ary2, "StringSplitBydef   " & 'Record definition -- 3')
Exit
; #FUNCTION# ========================================================================
; Name...........: _StringSplitBydef
; Description ...: Splits a string into substrings based on the record definition string.
;                  Returns an array, with each element, containing a given number of characters defined by the
;                  record definition string. Null fields are accepted.
;                  The last element will contain any remaining characters.
; Syntax.........: _StringSplitBydef($recString, $recdef)
; Parameters ....: $recString - string to be split.
;                  $recdef - Record definition string.
;                       Format 1 - a STRING - containing [index0 lenght[:]index1 lenght[:]index2 lenght[:]...[:]indexN lenght]
;                                           this will return an array with index lenghts defined by [indexN lenght]
;                       Format 2 - a NUMBER - this will return an array with equal index lenghts defined by [NUMBER]
; Return values .: Success - Returns a 0-based indexed array.
;                  Failure - Blank string and @error = 1
; Author ........: Atwolf359
; Modified.......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: _StringSplitBydef('0123456789abcdef', '4:3:0:2:1')
;                  _StringSplitBydef('0123456789abcdef', 3)
;
; ===========================================================
Func _StringSplitBydef($recString, $recdef)
    Local $i, $m, $n
    If IsNumber($recdef) = 1 Then
        $i = StringLen($recString)
        $recdef = Int($recdef)
        If $recdef < 1 Or $i < 1 Then
            $ary = ''
            SetError(1)
        Else
            $i = $i / $recdef
            $m = Int($i)
            If $m <> $i Then $m = $m + 1
            Local $ary[$m]
            For $i = 0 To $m - 1
                $ary[$i] = StringMid($recString, $i * $recdef + 1, $recdef)
            Next
        EndIf
    Else
        $ary = StringSplit($recdef, ':')
        $m = $ary[0]
        $n = 0
        For $i = 1 To $m
            $ary[$i - 1] = StringMid($recString, $n + 1, $ary[$i])
            $n = $n + $ary[$i]
        Next
        If $n < StringLen($recString) Then
            $ary[$m] = StringMid($recString, $n + 1)
        Else
            $i = UBound($ary) - 1
            ReDim $ary[$i]
        EndIf
    EndIf
    Return $ary
EndFunc   ;==>_StringSplitBydef

I've been on the net so long, I remember when I could only e-mail myself!

Share this post


Link to post
Share on other sites



This could be done easily with StringRegExp().


When the words fail... music speaks

Share this post


Link to post
Share on other sites

This could be done easily with StringRegExp().

That's great!

Show me.


I've been on the net so long, I remember when I could only e-mail myself!

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