By
argumentum
There is a _ArrayToString() but no _ArrayFromString(). ( searched in the forum and google )
The example is based on the _ArrayToString() help file, to show the reconstruction of the array.
#include <Array.au3>
#include <MsgBoxConstants.au3>
Local $aArray[20]
For $i = 0 To 19
$aArray[$i] = $i
Next
_ArrayDisplay($aArray, "1D Array")
MsgBox($MB_SYSTEMMODAL, "Items 1-7", _ArrayToString($aArray, @TAB, 1, 7))
ConsoleWrite('>' & _ArrayToString($aArray, @TAB, 1, 7) & '<' & @CRLF)
_ArrayDisplay(_ArrayFromString(_ArrayToString($aArray, @TAB, 1, 7), @TAB), "1D ArrayFromString")
Local $aArray[10][10]
For $i = 0 To 9
For $j = 0 To 9
$aArray[$i][$j] = $i & "-" & $j
Next
Next
_ArrayDisplay($aArray, "2D Array")
MsgBox($MB_SYSTEMMODAL, "Rows 4-7, cols 2-5", _ArrayToString($aArray, " :: ", 4, 7, @CRLF, 2, 5))
ConsoleWrite('>' & _ArrayToString($aArray, " :: ", 4, 7, @CRLF, 2, 5) & '<' & @CRLF)
_ArrayDisplay(_ArrayFromString(_ArrayToString($aArray, " :: ", 4, 7, @CRLF, 2, 5), " :: ", @CRLF), "2D ArrayFromString")
; au3.user.calltips.api:
; _ArrayFromString($sString , [$sDelim_Col = "|" [, $sDelim_Row = @CRLF [, $iForce2D = 0]]]) Rebuild an array from _ArrayToString()
Func _ArrayFromString($sString, $sDelim_Col = "|", $sDelim_Row = @CRLF, $iForce2D = 0,) ; https://www.autoitscript.com/forum/topic/197277-_arrayfromstring/
If $sDelim_Col = Default Or String($sDelim_Col) = "" Then $sDelim_Col = "|"
If $sDelim_Row = Default Or String($sDelim_Row) = "" Then $sDelim_Row = @CRLF
$iForce2D = Int($iForce2D)
Local $m, $n, $b, $c = StringSplit($sString, $sDelim_Row, 3) ; $STR_ENTIRESPLIT + $STR_NOCOUNT
$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
PS: so, how to save an array to an ini file ? ( small array, the limitations of an ini file still applies )
#include <Array.au3>; For _ArrayDisplay()
; if you declare it, it will use it, else, use default
;Global $g_iniFile = @ScriptDir & "\ThisTest.ini"
Example()
Func Example()
Local $n, $aTest, $aArray[3] = ["00", "one", "2"]
; if is not in the INI file, it will save it
$aTest = IniGet("Test", $aArray)
_ArrayDisplay($aTest, "1st")
; since is saved, it'll recall it
$aTest = IniGet("Test")
For $n = 0 To UBound($aTest) - 1 ; ..just to show the elements found as integer
If IsInt($aTest[$n]) Then $aTest[$n] &= " = IsInt() = " & (IsInt($aTest[$n]) = 1)
Next
_ArrayDisplay($aTest, "2nd")
EndFunc ;==>Example
Func IniGet($sKey, $vDefault = Default, $sSection = "Settings")
Local Static $ini = IsDeclared("g_iniFile") ? Eval("g_iniFile") : StringTrimRight(@ScriptFullPath, 4) & ".ini"
Local $v, $s = IniRead($ini, $sSection, $sKey, Chr(1))
If $s = Chr(1) Then
If $vDefault == Default Then
Return SetError(1, 0, "")
Else
IniSet($sKey, $vDefault, $sSection)
Return $vDefault
EndIf
EndIf
$v = StringLeft($s, 1)
Switch $v
Case "i"
Return Int(StringTrimLeft($s, 2))
Case "a"
Return _ArrayFromString(BinaryToString(StringTrimLeft($s, 2)), Chr(1), Chr(2))
Case "d"
Return Binary(StringTrimLeft($s, 2))
Case Else
Return String(StringTrimLeft($s, 2))
EndSwitch
EndFunc ;==>IniGet
Func IniSet($sKey, $vValue, $sSection = "Settings")
Local Static $ini = IsDeclared("g_iniFile") ? Eval("g_iniFile") : StringTrimRight(@ScriptFullPath, 4) & ".ini"
If IsInt($vValue) Then
$vValue = "i:" & $vValue
ElseIf IsArray($vValue) Then
$vValue = "a:" & StringToBinary(_ArrayToString($vValue, Chr(1), -1, -1, Chr(2)))
ElseIf IsBinary($vValue) Then
$vValue = "d:" & $vValue
Else
$vValue = "s:" & $vValue
EndIf
$vValue = IniWrite($ini, $sSection, $sKey, $vValue)
Return SetError(@error, @extended, $vValue)
EndFunc ;==>IniSet
; au3.user.calltips.api:
; _ArrayFromString($sString , [$sDelim_Col = "|" [, $sDelim_Row = @CRLF [, $iForce2D = 0]]]) Rebuild an array from _ArrayToString()
Func _ArrayFromString($sString, $sDelim_Col = "|", $sDelim_Row = @CRLF, $iForce2D = 0,) ; https://www.autoitscript.com/forum/topic/197277-_arrayfromstring/
If $sDelim_Col = Default Or String($sDelim_Col) = "" Then $sDelim_Col = "|"
If $sDelim_Row = Default Or String($sDelim_Row) = "" Then $sDelim_Row = @CRLF
$iForce2D = Int($iForce2D)
Local $m, $n, $b, $c = StringSplit($sString, $sDelim_Row, 3) ; $STR_ENTIRESPLIT + $STR_NOCOUNT
$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
PS2: https://www.autoitscript.com/trac/autoit/ticket/3696#ticket