Opened 7 years ago
Closed 6 years ago
#3696 closed Feature Request (Completed)
Func _ArrayFromString($s, $sDelim_Col = "|", $sDelim_Row = @CRLF, $iForce2D = 0)
| Reported by: | argumentum | Owned by: | J-Paul Mesnage |
|---|---|---|---|
| Milestone: | 3.3.15.4 | Component: | Standard UDFs |
| Version: | Severity: | None | |
| Keywords: | _ArrayFromString | Cc: |
Description
; #FUNCTION# ==================================================================================================================== ; Author ........: argumentum ; https://www.autoitscript.com/forum/topic/197277-_arrayfromstring/ ; Modified.......: ; =============================================================================================================================== Func _ArrayFromString($s, $sDelim_Col = "|", $sDelim_Row = @CRLF, $iForce2D = 0) Local $m, $n, $b, $c = StringSplit($s, $sDelim_Row, 3) $b = StringSplit($c[0], $sDelim_Col, 3) If UBound($c) = 1 And Not $iForce2D Then For $m = 0 To UBound($b) - 1 $b[$m] = StringStripWS($b[$m], 3) If $b[$m] == Int($b[$m]) Then $b[$m] = Int($b[$m]) Next Return $b EndIf Local $a[UBound($c)][UBound($b)] For $n = 0 To UBound($c) - 1 $b = StringSplit($c[$n], $sDelim_Col, 3) If UBound($b) > UBound($a, 2) Then Return SetError(1) For $m = 0 To UBound($b) - 1 $b[$m] = StringStripWS($b[$m], 3) If $b[$m] == Int($b[$m]) Then $b[$m] = Int($b[$m]) $a[$n][$m] = $b[$m] Next Next Return $a EndFunc ;==>_ArrayFromString
This function reconstructs an "array to text" created with _ArrayToString() and proposed change to _SQLite_Display2DResult() on ticket https://www.autoitscript.com/trac/autoit/ticket/3695#ticket.
Test code is at https://www.autoitscript.com/forum/topic/197277-_arrayfromstring/
This addition is not code braking.
Attachments (0)
Change History (7)
follow-up: 2 comment:1 by , 7 years ago
comment:2 by , 7 years ago
Replying to BrewManNH:
If $b[$m] == Int($b[$m])
Don't use the double equals sign in comparisons ...
Regardless of the "double equals" or not, the idea is sound and the code is functional as is.
If it needs correction, just change it on the final review.
comment:3 by , 7 years ago
The string to int. idea is just wrong. ( https://www.autoitscript.com/forum/topic/197277-_arrayfromstring/?do=findComment&comment=1419627 )
With those lines removed, the revised code would be:
Func _ArrayFromString($s, $sDelim_Col = "|", $sDelim_Row = @CRLF, $iForce2D = 0) ; https://www.autoitscript.com/forum/topic/197277-_arrayfromstring/ Local $m, $n, $b, $c = StringSplit($s, $sDelim_Row, 3) $b = StringSplit($c[0], $sDelim_Col, 3) If UBound($c) = 1 And Not $iForce2D Then Return $b Local $a[UBound($c)][UBound($b)] For $n = 0 To UBound($c) - 1 $b = StringSplit($c[$n], $sDelim_Col, 3) If UBound($b) > UBound($a, 2) Then Return SetError(1) For $m = 0 To UBound($b) - 1 $a[$n][$m] = $b[$m] Next Next Return $a EndFunc ;==>_ArrayFromString
comment:4 by , 6 years ago
| Owner: | set to |
|---|---|
| Status: | new → assigned |
comment:5 by , 6 years ago
this code should be ready to include ( unlike the prior posts )
; #FUNCTION# ==================================================================================================================== ; Name ..........: _ArrayFromString ; Description ...: Reconstruct an array from _ArrayToString() or _SQLite_Display2DResult()** ; Syntax ........: _ArrayFromString($sArrayStr[, $sDelim_Col = "|", [$sDelim_Row = @CRLF, [$bForce2D = False]]]) ; Parameters ....: $sArrayStr : A string formated by _ArrayToString() or _SQLite_Display2DResult()** ; $sDelim_Col : [optional] default is "|" ; $sDelim_Row : [optional] default is @CRLF ; $bForce2D : [optional] default is False. True will force a 2 dimensional array even if 1 dimensional. ; Return values .: Success - An array ; Failure - Return 0 with error = 1 ; ; Author ........: argumentum ; Remarks .......: ** for _SQLite_Display2DResult(), $sDelim_Col must be declared. ; =============================================================================================================================== Func _ArrayFromString($sArrayStr, $sDelim_Col = "|", $sDelim_Row = @CRLF, $bForce2D = False) If $sDelim_Col = Default Then $sDelim_Col = "|" If $sDelim_Row = Default Then $sDelim_Row = @CRLF If $bForce2D = Default Then $bForce2D = False Local $aRow, $aCol = StringSplit($sArrayStr, $sDelim_Row, 3) $aRow = StringSplit($aCol[0], $sDelim_Col, 3) If UBound($aCol) = 1 And Not $bForce2D Then For $m = 0 To UBound($aRow) - 1 $aRow[$m] = StringStripWS($aRow[$m], 3) If $aRow[$m] == Int($aRow[$m]) Then $aRow[$m] = Int($aRow[$m]) Next Return $aRow EndIf Local $aRet[UBound($aCol)][UBound($aRow)] For $n = 0 To UBound($aCol) - 1 $aRow = StringSplit($aCol[$n], $sDelim_Col, 3) If UBound($aRow) > UBound($aRet, 2) Then Return SetError(1) For $m = 0 To UBound($aRow) - 1 $aRow[$m] = StringStripWS($aRow[$m], 3) If $aRow[$m] == Int($aRow[$m]) Then $aRow[$m] = Int($aRow[$m]) $aRet[$n][$m] = $aRow[$m] Next Next Return $aRet EndFunc ;==>_ArrayFromString
comment:6 by , 6 years ago
..added $iStripWS option
; #FUNCTION# ==================================================================================================================== ; Name ..........: _ArrayFromString ; Description ...: Reconstruct an array from _ArrayToString() or _SQLite_Display2DResult()** ; Syntax ........: _ArrayFromString($sArrayStr[, $sDelim_Col = "|", [$sDelim_Row = @CRLF, [$bForce2D = False, [$iStripWS = 3]]]]) ; Parameters ....: $sArrayStr : A string formated by _ArrayToString() or _SQLite_Display2DResult()** ; $sDelim_Col : [optional] default is "|" ; $sDelim_Row : [optional] default is @CRLF ; $bForce2D : [optional] default is False. True will force a 2 dimensional array even if 1 dimensional. ; $iStripWS : [optional] default is 3. This is the flag for StringStripWS(). Set to zero to disable. ; Return values .: Success - An array ; Failure - Return 0 with error = 1 ; ; Author ........: argumentum ; Remarks .......: ** for _SQLite_Display2DResult(), $sDelim_Col must be declared. ; =============================================================================================================================== Func _ArrayFromString($sArrayStr, $sDelim_Col = "|", $sDelim_Row = @CRLF, $bForce2D = False, $iStripWS = 3) If $sDelim_Col = Default Then $sDelim_Col = "|" If $sDelim_Row = Default Then $sDelim_Row = @CRLF If $bForce2D = Default Then $bForce2D = False If $iStripWS = Default Then $iStripWS = 3 ; $STR_STRIPLEADING + $STR_STRIPTRAILING Local $aRow, $aCol = StringSplit($sArrayStr, $sDelim_Row, 3) $aRow = StringSplit($aCol[0], $sDelim_Col, 3) If UBound($aCol) = 1 And Not $bForce2D Then For $m = 0 To UBound($aRow) - 1 $aRow[$m] = ($iStripWS ? StringStripWS($aRow[$m], $iStripWS) : $aRow[$m]) Next Return $aRow EndIf Local $aRet[UBound($aCol)][UBound($aRow)] For $n = 0 To UBound($aCol) - 1 $aRow = StringSplit($aCol[$n], $sDelim_Col, 3) If UBound($aRow) > UBound($aRet, 2) Then Return SetError(1) For $m = 0 To UBound($aRow) - 1 $aRet[$n][$m] = ($iStripWS ? StringStripWS($aRow[$m], $iStripWS) : $aRow[$m]) Next Next Return $aRet EndFunc ;==>_ArrayFromString
comment:7 by , 6 years ago
| Milestone: | → 3.3.15.4 |
|---|---|
| Owner: | changed from to |
| Resolution: | → Completed |
| Status: | assigned → closed |
Added by revision [12350] in version: 3.3.15.4

Don't use the double equals sign in comparisons unless you want to do a case sensitive comparison, and in this example from your code would be silly to do.