Opened 6 years ago
Closed 4 years ago
#3696 closed Feature Request (Completed)
Func _ArrayFromString($s, $sDelim_Col = "|", $sDelim_Row = @CRLF, $iForce2D = 0)
Reported by: | argumentum | Owned by: | Jpm |
---|---|---|---|
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)
comment:1 follow-up: ↓ 2 Changed 6 years ago by BrewManNH
comment:2 in reply to: ↑ 1 Changed 6 years ago by argumentum
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 Changed 6 years ago by argumentum
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 Changed 5 years ago by Jpm
- Owner set to BrewManNH
- Status changed from new to assigned
comment:5 Changed 4 years ago by argumentum
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 Changed 4 years ago by argumentum
..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 Changed 4 years ago by Jpm
- Milestone set to 3.3.15.4
- Owner changed from BrewManNH to Jpm
- Resolution set to Completed
- Status changed from assigned to closed
Added by revision [12350] in version: 3.3.15.4
Guidelines for posting comments:
- You cannot re-open a ticket but you may still leave a comment if you have additional information to add.
- In-depth discussions should take place on the forum.
For more information see the full version of the ticket guidelines here.
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.