Jump to content

Recommended Posts

Edit: This is now part of beta 3.3.15.4
__________________________________________________________________________________________________

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()
; #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
; Link...........: https://www.autoitscript.com/forum/topic/197277-_arrayfromstring/
; 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


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()
; #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
; Link...........: https://www.autoitscript.com/forum/topic/197277-_arrayfromstring/
; 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


PS2: https://www.autoitscript.com/trac/autoit/ticket/3696#ticket <-- completed.

Edited by argumentum
better code

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

certainly different from stringsplit with the second delimiter, it's more akin to passing a string to _ArrayAdd.

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

5 minutes ago, iamtheky said:

akin to passing a string to _ArrayAdd

Yeap.  If I was to find that, I would not have written this.  In the help file there is no "related" link to one another, and the name itself does not reflect the possibility.  :>

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

They do have a difference, in that yours builds the array based off input rather than trying to cram the input into the existing dimensions.  There are cases where this would be desirable behavior and clean up a lot of prep work. 

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

@jchd, could you modify the _SQLite_Display2DResult() as shown in the comment in the code ? ( I've done it for my use but I think is a good idea for everyone )
I dump to text some db tables and I like the human readable output but I wanted the possibility to reconstruct the array.  With this additional parameter I can do it easily. 

#include <Array.au3>
#include <SQLite.au3>


#cs

on SQLite.au3, modify these two places

from:
Func _SQLite_Display2DResult($aResult, $iCellWidth = 0, $bReturn = False)
to:
Func _SQLite_Display2DResult($aResult, $iCellWidth = 0, $bReturn = False, $sDelim_Col = "")

from:
$sOut &= StringFormat(" %-" & $iCellWidthUsed & "." & $iCellWidthUsed & "s ", $aResult[$iRow][$iCol])
to:
$sOut &= StringFormat(" %-" & $iCellWidthUsed & "." & $iCellWidthUsed & "s ", $aResult[$iRow][$iCol]) & $sDelim_Col


#ce


Local $sDisplay2DResult, $aArray[10][10]
For $i = 0 To 9
    For $j = 0 To 9
        $aArray[$i][$j] = $i & "-" & $j
    Next
Next

$sDisplay2DResult = _SQLite_Display2DResult($aArray, 0, True, @TAB) ; with the above mod.
ConsoleWrite($sDisplay2DResult)
_ArrayDisplay(_ArrayFromString(StringTrimRight($sDisplay2DResult, 4), " " & @TAB & " ", " " & @TAB & @CRLF))

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

Thanks  :)
PS: https://www.autoitscript.com/trac/autoit/ticket/3695#ticket

Edited by argumentum
better code

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

I'm not able to maintain code anymore so you should post a ticket in Trac about that. In the meantime, change your own copy of the UDF.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

  • 4 weeks later...

In looking at the code in the _ArrayFromString I have a question. what this line is supposed to be doing?

If $b[$m] == Int($b[$m]) Then $b[$m] = Int($b[$m])

You're checking an entry in the $b array against the string value of the Integer of that entry? And, if the value is equal, you set it to the integer value of the integer you just checked?The only time that line will return true is if $b[m] is a whole number, either a string value or a numeric value, so why does it need to be set to a whole number if it's already a whole number?

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

2 hours ago, BrewManNH said:

I have a question

Global $s = "00"
If $s Then MsgBox(0, @ScriptName, "zero")
If $s = Int($s) Then MsgBox(0, @ScriptName, "equal")
If $s == Int($s) Then MsgBox(0, @ScriptName, "not equal") ; the reason for double equal
$s = Int($s)
If $s Then MsgBox(0, @ScriptName, "nothing") ; the reason for String2Integer

 

Edited by argumentum
spelling

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

Silently removing leading zeroes may permanently destroy data, never do that in the general case. Leading zeroes are semantically significant in an unbounded number of situations: James is "007", not "7" and 13-digit barcode "0000870361925" may designate a completely distinct item from "870361925".

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

..at the time I came up with the idea, it made sense for what I was doing, but "James 7" is just wrong.
So due to overwhelming resistance to the idea due to "preponderance of the evidence", I'll remove the lines that makes this not fit for general purpose.
I recon the user can integerify if is needed.

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

I didn't say that to annoy you ;)

Just think of a string is a file/folder name or relative URL or time stamp ("00:13:43") having leading zero(es). It's always correct to avoid presuming the nature of user data when we have no clue what it really is and means.

Integerifying number-like strings isn't even required in most use case under AutoIt: ConsoleWrite("123" + "00444" & @LF) works transparently.

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

@jchd, I am not annoyed, am grateful. I am pushing for this function to be included in the distribution as I find it complementary to other functions.
Comments from @BrewManNH and @user4157124 are also welcomed.
If y'all don't share your views, I may be going on believing this is kosher but eventually find myself scratching my head. Or worse, someone else, trusting this code, stuff don't work.

So as a defacto, prove me wrong. I'll be happy and grateful.

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
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
 Share

×
×
  • Create New...