Renderer Posted March 9, 2019 Posted March 9, 2019 Hi everyone! I have got stuck with a small problem. I am trying to split the following string into a 2d array: $iString = "Bob,24|Jane,25|Julian,26|" I want to have the output like: local $2D_Array[3][2] = [["Bob",24],["Jane",25],["Julian",26]] The code I've tried is: func StringSplit2D($iString,$iSep1,$iSep2) local $iSplit1 = StringSplit($iString,$iSep1) for $i = 0 to UBound($iSplit1) -1 local $2D_Array[UBound($iSplit1)][UBound($iSplit1[$i])] = [$iSplit1,$iSplit1[$i]] Next return $2d_Array EndFunc MsgBox(18,'',StringSplit2D("Bob,24|Jane,25|Julian,26|","|","1")) The "|" is the main separator, which splits the string into Rows and the "," is the secondary separator, that shall split the string into columns. I have never worked too much with 2d Arrays so maybe you could provide me some help with your answers. Thanks in advance.
iamtheky Posted March 9, 2019 Posted March 9, 2019 (edited) $iString = "Bob,24|Jane,25|Julian,26|" msgbox(0, '' , 'local $2D_Array[3][2] = [["' & stringreplace(stringtrimright(stringreplace($iString , "," , '",') , 1) , "|" , '],["') & ']]') wait sorry, thats the literal....let me make that an array now. apologies. #include<array.au3> $iString = "Bob,24|Jane,25|Julian,26|" local $2D_Array[0][2] _ArrayAdd($2D_Array , StringTrimRight($iString , 1) , 0 , "," , "|") _ArrayDisplay($2D_Array) Edited March 9, 2019 by iamtheky ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__)
boludoz Posted April 17, 2021 Posted April 17, 2021 (edited) #include <StringConstants.au3> #include <Array.au3> Global $g_sString = "Bob,24|Jane,25|Julian,26|" Global $g_sDelimRow = '|' Global $g_sDelimCol = ',' Global $g_aArray2D = StringSplit2D($g_sString, $g_sDelimCol, $g_sDelimRow) _ArrayDisplay($g_aArray2D, @ScriptName) Func StringSplit2D($sMatches = "Hola-2-5-50-50-100-100|Hola-6-200-200-100-100", Const $sDelim_Item = "-", Const $sDelim_Row = "|", $bFixLast = Default) Local $iValDim_1, $iValDim_2 = 0, $iColCount ; Fix last item or row. If $bFixLast <> False Then Local $sTrim = StringRight($sMatches, 1) If $sTrim = $g_sDelimRow Or $sTrim = $sDelim_Item Then $sMatches = StringTrimRight($sMatches, 1) EndIf Local $aSplit_1 = StringSplit($sMatches, $sDelim_Row, $STR_NOCOUNT + $STR_ENTIRESPLIT) $iValDim_1 = UBound($aSplit_1, $UBOUND_ROWS) Local $aTmp[$iValDim_1][0], $aSplit_2 For $i = 0 To $iValDim_1 - 1 $aSplit_2 = StringSplit($aSplit_1[$i], $sDelim_Item, $STR_NOCOUNT + $STR_ENTIRESPLIT) $iColCount = UBound($aSplit_2) If $iColCount > $iValDim_2 Then $iValDim_2 = $iColCount ReDim $aTmp[$iValDim_1][$iValDim_2] EndIf For $j = 0 To $iColCount - 1 $aTmp[$i][$j] = $aSplit_2[$j] Next Next Return $aTmp EndFunc ;==>StringSplit2D It is based on array add but does not need ByRef, it is very stable and has been tested millions of times, it will fix the blank spaces. Edited April 17, 2021 by boludoz
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now