Jump to content

Challenge! Experts only!


Recommended Posts

Greetings,

I build a little script to dump anything...

Like this...
 

Local $arr1[] = ["a", "b", "c"]

dump(arr1[]), print:
~[
~~~a, b, c
~]


 

Local $arr1[] = ["a", "b", "c"]
Local $arr2[] = ["d", "e", "f"]
Local $arr5[] = [$arr1, $arr2]

dump(arr5[]), print:
~[
~~~[
~~~~~~a, b, c
~~~], [
~~~~~~d, e, f
~~~]
~]



But this is wrong!

I want this:
 

~[
~~~[
~~~~~~a, b, c
~~~],
~~~[
~~~~~~d, e, f
~~~]
~]

 

Follow the script:

#include-once
#include <Array.au3>
#include <String.au3>

Local $arr1[] = ["a", "b", "c"]
Local $arr2[] = ["d", "e", "f"]
Local $arr3[][4] = [["e", "f", "g", "h"], ["i", "j", "k", "l"], ["m", "n", "o", "p"]]
Local $oo = ObjCreate("Scripting.Dictionary")
$oo.Add("key1", "value1")
$oo.Add("key2", "value2")

Local $arr4[] = [$arr1, $arr2, $oo, "q"]
Local $arr5[] = [$arr1, $arr2]

Local $dump = dump($arr1)
ConsoleWrite($dump & @LF)
;~ ConsoleWrite("@@@@@@@@@@@@@@@" & @LF)
;~ Local $dump = dump($arr3)
;~ ConsoleWrite($dump & @LF)
;~ ConsoleWrite("@@@@@@@@@@@@@@@" & @LF)
;~ Local $dump = dump($arr4)
;~ ConsoleWrite($dump & @LF)
ConsoleWrite("@@@@@@@@@@@@@@@" & @LF)
Local $dump = dump($arr5)
ConsoleWrite($dump & @LF)

Func dump($var = "", $spc = 0, $lvl = 0)
    Local $ret = "", $tab = ""
    If IsArray($var) Then
        Switch UBound($var, 0)
            Case 1
                $spc = 1 ; its only to test with any case, any spc, in production, this not exist
                Local $size1 = UBound($var, 1) - 1
                $tab = spc($spc)

                If $lvl Then
                    $ret &= "[" & @LF & $tab
                    For $ii1 = 0 To $size1
                        If $ii1 = 0 Then
                            $ret &= $tab & spc(4) & dump($var[$ii1], $spc + 1, $lvl + 1) & ", "
                        ElseIf $ii1 = $size1 Then
                            $ret &= dump($var[$ii1], $spc + 1, $lvl + 1)
                        Else
                            $ret &= dump($var[$ii1], $spc + 1, $lvl + 1) & ", "
                        EndIf
                    Next
                    $ret &= @LF & $tab & spc(2) & "]"
                Else
                    $ret &= $tab & "[" & @LF & $tab
                    For $ii1 = 0 To $size1
                        If $ii1 = 0 Then
                            $ret &= spc(2) & dump($var[$ii1], $spc + 1, $lvl + 1) & ", "
                        ElseIf $ii1 = $size1 Then
                            $ret &= dump($var[$ii1], $spc + 1, $lvl + 1)
                        Else
                            $ret &= dump($var[$ii1], $spc + 1, $lvl + 1) & ", "
                        EndIf
                    Next
                    $ret &= @LF & $tab & "]"
                EndIf
            Case 2
                Local $size1 = UBound($var, 1) - 1
                Local $size2 = UBound($var, 2) - 1
                $tab = spc($spc)
                $ret &= $tab & "[" & @LF
                For $ii1 = 0 To $size1
                    $ret &= $tab & spc(2) & "[" & @LF
                    For $ii2 = 0 To $size2
                        If $ii2 = 0 Then
                            $ret &= $tab & spc(4) & dump($var[$ii1][$ii2], $spc + 2) & ", "
                        ElseIf $ii2 = $size2 Then
                            $ret &= dump($var[$ii1][$ii2], $spc + 2)
                        Else
                            $ret &= dump($var[$ii1][$ii2], $spc + 2) & ", "
                        EndIf
                    Next
                    If $ii1 = $size1 Then
                        $ret &= @LF & $tab & spc(2) & "]" & @LF
                    Else
                        $ret &= @LF & $tab & spc(2) & "]," & @LF
                    EndIf

                Next
                $ret &= $tab & "]" & @LF
        EndSwitch
    Else
        If IsObj($var) Then
            Local $count1 = $var.Count - 1
            Local $aKeys = $var.Keys
            $ret &= "{" & @LF
            For $ii1 = 0 To $count1
                $ret &= $aKeys[$ii1] & ' = ' & dump($var.Item($aKeys[$ii1]), $spc + 2)
                $ret &= ($ii1 = $count1) ? "" : @LF
            Next
            $ret &= @LF & "}" & @LF
        Else
            $ret &= $var
        EndIf
    EndIf
    Return $ret
EndFunc   ;==>dump

Func spc($n = 0)
    Return $n ? _StringRepeat("~", $n) : ""
EndFunc   ;==>spc

 

Edited by Luigi

Visit my repository

Link to comment
Share on other sites

Here is my attempt?

#include-once
#include <Array.au3>
#include <String.au3>

Local $oo = ObjCreate("Scripting.Dictionary")
    $oo.Add("key1", "value1")
    $oo.Add("key2", "value2")
Local $aArray1[] = ["a", "b", "c"]
Local $aArray2[] = ["d", "e", "f"]
Local $aArray3[][4] = [["e", "f", "g", "h"], ["i", "j", "k", "l"], ["m", "n", "o", "p"]]
Local $aArray4[] = [$aArray1, $aArray2, $aArray3, $oo, "q"]

Local $sPrintItems = _PrintItems($aArray4)
ConsoleWrite($sPrintItems & @CRLF)

Func _PrintItems($aArray, $iSpace = 0, $iLevel = 0)
    Local $vArrayItem, $vObjectItem, $bArrayItem = False
    If IsArray($aArray) Then
        $sPrint = _Space(1) & "[" & @CRLF
        For $i = 0 To UBound($aArray) - 1
            $vArrayItem = $aArray[$i]
            If IsArray($vArrayItem) Then
                ;~ Print Sub Array
                $sPrint &= _Space(3) & "[" & @CRLF
                $sPrint &= _Space(6) & _ArrayToString($vArrayItem, ", ", -1, -1, @CRLF & _Space(6)) & @CRLF
                $sPrint &= $i = UBound($aArray) - 1 ? _Space(3) & "]" & @CRLF : _Space(3) & "]," & @CRLF
                $bArrayItem = False
            ElseIf IsObj($vArrayItem) Then
                ;~ Print Object
                $sPrint &= _Space(3) & "[" & @CRLF
                For $vObjectItem In $vArrayItem.Keys
                    $sPrint &= _Space(6) & $vObjectItem & " = " & $vArrayItem($vObjectItem) & @CRLF
                Next
                $sPrint &= $i = UBound($aArray) - 1 ? _Space(3) & "]" & @CRLF : _Space(3) & "]," & @CRLF
                $bArrayItem = False
            Else
                ;~ Print Array
                If $bArrayItem Then ContinueLoop
                $sPrint &= _Space(3) & "[" & @CRLF
                $sPrint &= _Space(6) & _ArrayToString($aArray, ", ") & @CRLF
                $sPrint &= _Space(3) & "]" & @CRLF
                $bArrayItem = True
            EndIf
        Next
        $sPrint &= _Space(1) & "]" & @CRLF
        Return $sPrint
    EndIf
EndFunc

Func _Space($n = 0)
    Return $n ? _StringRepeat("~", $n) : ""
EndFunc   ;==>_Space

 

Link to comment
Share on other sites

I can this...
But is so complex yet... But work with this examples...
 

#include-once
#include <Array.au3>
#include <String.au3>
#include <Crypt.au3>

_Crypt_Startup()

OnAutoItExitRegister("OnExit")

Local $arr1[] = ["a", "b", "c", "d"]
Local $arr2[] = ["e", "f", "g", "h"]
Local $arr3[] = ["i", "j", "k", "l"]
Local $arr4[] = [$arr1, $arr2]
Local $arr5[][3] = [["!", "@", "#"], ["$", "%", "+"], ["&", "*", "("]]
Local $oo = ObjCreate("Scripting.Dictionary")
$oo.Add("key1", "value1")
$oo.Add("key2", "value2")

Local $str1 = dump($arr1)
ConsoleWrite($str1)
ConsoleWrite("---------------" & @LF)

Local $str4 = dump($arr4)
ConsoleWrite($str4)
ConsoleWrite("---------------" & @LF)

Local $str5 = dump($arr5)
ConsoleWrite($str5)
ConsoleWrite("---------------" & @LF)

Local $aCheck[2] = [DataMD5($str5), IniRead("md5.ini", "config", "arr5", "@empty")]

If $aCheck[0] = $aCheck[1] Then
    ConsoleWrite("+md5[ equal ]" & @LF)
Else
    ConsoleWrite("-md5[ diff ]" & @LF)
EndIf

IniWrite("md5.ini", "config", "arr5", $aCheck[0])




Func dump($var = "", $space = 0, $comma = "")
    Local $ret = ""
    If IsArray($var) Then
        Switch UBound($var, 0)
            Case 1
                Local $size1 = UBound($var, 1) - 1
                $ret &= _Space($space) & "[" & @LF
                For $ii1 = 0 To $size1
                    If $ii1 = 0 Then
                        Switch VarGetType($var[$ii1])
                            Case "Array"
                                $ret &= dump($var[$ii1], $space + 2, ",")
                            Case Else
                                $ret &= _Space($space + 2) & dump($var[$ii1], $space + 2)
                        EndSwitch
                    ElseIf $ii1 = $size1 Then
                        Switch VarGetType($var[$ii1])
                            Case "Array"
                                $ret &= dump($var[$ii1], $space + 2)
                            Case Else
                                $ret &= ", " & dump($var[$ii1], $space) & @LF
                        EndSwitch
                    Else
                        $ret &= ", " & dump($var[$ii1], $space + 2)
                    EndIf
                Next
                $ret &= _Space($space) & "]" & $comma & @LF
            Case 2
                Local $size1 = UBound($var, 1) - 1
                Local $size2 = UBound($var, 2) - 1
                $ret &= _Space($space) & "[" & @LF
                For $ii1 = 0 To $size1
                    $ret &= _Space($space + 2) & "[" & @LF
                    For $ii2 = 0 To $size2
                        If $ii1 = 0 Then
                            If $ii2 = 0 Then
                                Switch VarGetType($var[$ii1][$ii2])
                                    Case "Array"
                                        $ret &= dump($var[$ii1][$ii2])
                                    Case Else
                                        $ret &= _Space($space + 4) & dump($var[$ii1][$ii2])
                                EndSwitch
                            ElseIf $ii2 = $size2 Then
                                Switch VarGetType($var[$ii1][$ii2])
                                    Case "Array"
                                        $ret &= ", " & dump($var[$ii1][$ii2])
                                    Case Else
                                        $ret &= ", " & dump($var[$ii1][$ii2]) & @LF
                                EndSwitch
                            Else
                                $ret &= ", " & dump($var[$ii1][$ii2])
                            EndIf
                        ElseIf $ii1 = $size1 Then
                            If $ii2 = 0 Then
                                Switch VarGetType($var[$ii1][$ii2])
                                    Case "Array"
                                        $ret &= dump($var[$ii1][$ii2])
                                    Case Else
                                        $ret &= _Space($space + 4) & dump($var[$ii1][$ii2])
                                EndSwitch
                            ElseIf $ii2 = $size2 Then
                                Switch VarGetType($var[$ii1][$ii2])
                                    Case "Array"
                                        $ret &= ", " & dump($var[$ii1][$ii2])
                                    Case Else
                                        $ret &= ", " & dump($var[$ii1][$ii2]) & @LF
                                EndSwitch
                            Else
                                $ret &= ", " & dump($var[$ii1][$ii2])
                            EndIf
                        Else
                            If $ii2 = 0 Then
                                $ret &= _Space($space + 4) & dump($var[$ii1][$ii2])
                            ElseIf $ii2 = $size2 Then
                                $ret &= ", " & dump($var[$ii1][$ii2]) & @LF
                            Else
                                $ret &= ", " & dump($var[$ii1][$ii2])
                            EndIf
                        EndIf
                    Next
                    If $ii1 = $size1 Then
                        $ret &= _Space($space + 2) & "]" & $comma & @LF
                    Else
                        $ret &= _Space($space + 2) & "]" & "," & @LF
                    EndIf
                Next
                $ret &= _Space($space) & "]" & $comma & @LF
        EndSwitch
    Else

        Switch VarGetType($var)
            Case "String"
                $ret = '"' & $var & '"'
            Case Else
                $ret = $var
        EndSwitch
    EndIf
    Return $ret
EndFunc   ;==>dump

Func _Space($n = 0)
    Return $n ? _StringRepeat(" ", $n) : ""
EndFunc   ;==>_Space

Func OnExit()
    _Crypt_Shutdown()
EndFunc   ;==>OnExit

Func DataMD5($var = "")
    Local $dHash = _Crypt_HashData($var, $CALG_MD5)
    If @error Then Return SetError(@error, 0, -1)
    Return $dHash
EndFunc   ;==>DataMD5

 

Output:

[
  "a", "b", "c", "d"
]
---------------
[
  [
    "a", "b", "c", "d"
  ],
  [
    "e", "f", "g", "h"
  ]
]
---------------
[
  [
    "!", "@", "#"
  ],
  [
    "$", "%", "+"
  ],
  [
    "&", "*", "("
  ]
]
---------------

Edited by Luigi

Visit my repository

Link to comment
Share on other sites

Just change:

Else
                ;~ Print Array
                If $bArrayItem Then ContinueLoop
                $sPrint &= _Space(3) & "[" & @CRLF
                $sPrint &= _Space(6) & _ArrayToString($aArray, ", ") & @CRLF
                $sPrint &= _Space(3) & "]" & @CRLF
                $bArrayItem = True
            EndIf

To

Else
                ;~ Print Array
                If $bArrayItem Then ContinueLoop
                $sPrint &= _Space(3) & _ArrayToString($aArray, ", ") & @CRLF
                $bArrayItem = True
            EndIf

 

Link to comment
Share on other sites

@Subz, very good and small your code!

I will studie, thank you.
 

#include-once
#include <Array.au3>
#include <String.au3>

; Author...: Subz
; Source...: https://www.autoitscript.com/forum/topic/198476-challenge-experts-only/?tab=comments#comment-1424189


; test #1
Local $sPrintItems = _PrintItems(1234)
ConsoleWrite($sPrintItems & @LF & @LF)

; test #2
Local $sPrintItems = _PrintItems("5678")
ConsoleWrite($sPrintItems & @LF & @LF)

; test #3
Local $sPrintItems = _PrintItems("abcd")
ConsoleWrite($sPrintItems & @LF & @LF)

; test #3
Local $arr1[] = [1, 2, 3]
Local $sPrintItems = _PrintItems($arr1)
ConsoleWrite($sPrintItems & @LF & @LF)

; test #4
Local $arr2[] = [11, 22, "aa", "bb"]
Local $sPrintItems = _PrintItems($arr2)
ConsoleWrite($sPrintItems & @LF & @LF)

; test #5
Local $arr4[] = ["a", "b", "c"]
Local $arr5[] = ["d", "e", "f"]
Local $arr6[] = [$arr4, $arr5]
Local $sPrintItems = _PrintItems($arr6)
ConsoleWrite($sPrintItems & @LF & @LF)

; test #6
Local $oo = ObjCreate("Scripting.Dictionary")
$oo.Add("key1", "value1")
$oo.Add("key2", "value2")
Local $arr7[] = [$arr4, $arr5, $oo]
Local $sPrintItems = _PrintItems($arr7)
ConsoleWrite($sPrintItems & @LF & @LF)


Func _PrintItems($aArray, $iiSpace = 0)
    Local $vArrayItem, $vObjectItem, $bArrayItem = False
    If IsArray($aArray) Then
        $sPrint = _Space(1) & "[" & @CRLF
        For $ii = 0 To UBound($aArray) - 1
            $vArrayItem = $aArray[$ii]
            If IsArray($vArrayItem) Then
;~ Print Sub Array
                $sPrint &= _Space(3) & "[" & @CRLF
                $sPrint &= _Space(6) & _ArrayToString($vArrayItem, ", ", -1, -1, @CRLF & _Space(6)) & @CRLF
                $sPrint &= $ii = UBound($aArray) - 1 ? _Space(3) & "]" & @CRLF : _Space(3) & "]," & @CRLF
                $bArrayItem = False
            ElseIf IsObj($vArrayItem) Then
;~ Print Object
                $sPrint &= _Space(3) & "[" & @CRLF
                For $vObjectItem In $vArrayItem.Keys
                    $sPrint &= _Space(6) & $vObjectItem & " = " & $vArrayItem($vObjectItem) & @CRLF
                Next
                $sPrint &= $ii = UBound($aArray) - 1 ? _Space(3) & "]" & @CRLF : _Space(3) & "]," & @CRLF
                $bArrayItem = False
            Else
;~ Print Array
                If $bArrayItem Then ContinueLoop
                $sPrint &= _Space(3) & _ArrayToString($aArray, ", ") & @CRLF
                $bArrayItem = True
            EndIf
        Next
        $sPrint &= _Space(1) & "]" & @CRLF
        Return $sPrint
    Else
        Return _PrintItemsType($aArray)
    EndIf
EndFunc   ;==>_PrintItems

Func _PrintItemsType($var = "")
    Switch VarGetType($var)
        Case "String"
            Return '"' & $var & '"'
        Case Else
            Return $var
    EndSwitch
EndFunc   ;==>_PrintItemsType

Func _Space($n = 0)
    Return $n ? _StringRepeat("~", $n) : ""
EndFunc   ;==>_Space

 

Visit my repository

Link to comment
Share on other sites

@Luigi, maybe you could use/adapt/adopt my version:

 

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

That is an AutoIt array⁽¹⁾ (arrays use integral indices) not an AutoIt Map (native associative array datatype similar to scripting dictionnary, proposed in the latest beta versions, but dismissed since).

That's why my script as it is needs the beta to run (many people have code using Maps, even now). Else remove or comment out the code part dealing with Maps: the Case "Map" block in __VarDump() and the function __VarDumpMap().

⁽¹⁾ The fact that your example code works is just by chance: an array index is a non-negative integer datatype, and when you invoke $arr["um"] AutoIt duck typing converts the string "um" to an integer under the hood, yielding 0. So your code is equivalent to $arr[0] which is legit. Thus $arr[<any string that converts to zero>] means $arr[0].

Edited by jchd
Clarity

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

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...