Jump to content

buggy 2d arrays


Yeik
 Share

Recommended Posts

I am trying to get specific arrays from a 2d array. will i have to create my own function for this or is there one already out there?

I have noticed some odd things with arrays and 2d arrays

global $arr1[2][2]= [[1, "one"], [2, "two"] ]

local $arr2 = $arr[1]; doesn't work
$arrvar = $arr1[1][0]; works to get the 2 from the second array


;The above doesn't work for what i wanted it for. But i found another way to do it

$var[2]= [1, "one"]
$var1[2]= [2, "two"]

$2darr[2]= [$var, $var1];if you do this though you can't use _arraydisplay()

;then you can later pull out either array into its own again, i haven't tried doing something like

$2darr[0][1]; it might work im not sure

_arraydisplay($2darr);shows up correctly as an array BUT doesn't show the 2d arrays as it would for $arr1

It would be nice if they would be identified as arrays either way, as it should be (an array is always an array as far as I'm concerned), unless you are having it act more like a mapping. which is completely different.

Anybody have any ideas or just what I have is the best it gets?

EDIT: Got home and could properly post and clean up my code

Edited by Yeik
func get_quote()
   local $quote 
   switch random(1, 3, 1)
    case 1
     $quote = '"' & "A human being should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, " & _
       "design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give " & _
       "orders, cooperate, act alone, solve equations, analyze a new problem, pitch manure, program a computer, cook a tasty meal, " & _
       "fight efficiently, die gallantly. Specialization is for insects." & '"' & " Robert A. Heinlein"
    case 2
     $quote =  '"' & "Within each of us lies the power of our consent to health and sickness, to riches and poverty, to freedom " & _
       "and to slavery. It is we who control these, and not another." & '"' & " Richard Bach (Illusions)"
    case 3
     $quote ='"' & "Don't handicap your children by making their lives easy." & '"' & " Robert A. Heinlein"
   EndSwitch
   MsgBox(0, "Quote for the moment", $quote & @CRLF)
EndFunc

get_quote()
Link to comment
Share on other sites

*BUMP for changes i made and edit i did*

func get_quote()
   local $quote 
   switch random(1, 3, 1)
    case 1
     $quote = '"' & "A human being should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, " & _
       "design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give " & _
       "orders, cooperate, act alone, solve equations, analyze a new problem, pitch manure, program a computer, cook a tasty meal, " & _
       "fight efficiently, die gallantly. Specialization is for insects." & '"' & " Robert A. Heinlein"
    case 2
     $quote =  '"' & "Within each of us lies the power of our consent to health and sickness, to riches and poverty, to freedom " & _
       "and to slavery. It is we who control these, and not another." & '"' & " Richard Bach (Illusions)"
    case 3
     $quote ='"' & "Don't handicap your children by making their lives easy." & '"' & " Robert A. Heinlein"
   EndSwitch
   MsgBox(0, "Quote for the moment", $quote & @CRLF)
EndFunc

get_quote()
Link to comment
Share on other sites

This?

BUT you can't do that with 2d array (the 2nd array), if you read my example above. (and now a new one below)

Dim $2darr[2][2]=[["this is a", "two dimension array]", ["this is the second array", "in a two dimension array"]]

;you can copy the 2d array, but not the second array, to get the array ["this is a", "two dimension array"] from that you have to do
Dim $myarray=[$2darr[0][0], $2darr[0][1]]; 

;however you can't retrieve it this way
Dim $arr = $2darr[0]; logical thinking would assume it would just pass the array as that is what is located in that spot.
Edited by Yeik
func get_quote()
   local $quote 
   switch random(1, 3, 1)
    case 1
     $quote = '"' & "A human being should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, " & _
       "design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give " & _
       "orders, cooperate, act alone, solve equations, analyze a new problem, pitch manure, program a computer, cook a tasty meal, " & _
       "fight efficiently, die gallantly. Specialization is for insects." & '"' & " Robert A. Heinlein"
    case 2
     $quote =  '"' & "Within each of us lies the power of our consent to health and sickness, to riches and poverty, to freedom " & _
       "and to slavery. It is we who control these, and not another." & '"' & " Richard Bach (Illusions)"
    case 3
     $quote ='"' & "Don't handicap your children by making their lives easy." & '"' & " Robert A. Heinlein"
   EndSwitch
   MsgBox(0, "Quote for the moment", $quote & @CRLF)
EndFunc

get_quote()
Link to comment
Share on other sites

BUT you can't do that with 2d array (the 2nd array), if you read my example above. (and now a new one below)

Dim $2darr[2][2]=[["this is a", "two dimension array]", ["this is the second array", "in a two dimension array"]]

;you can copy the 2d array, but not the second array, to get the array ["this is a", "two dimension array"] from that you have to do
Dim $myarray=[$2darr[0][0], $2darr[0][1]]; 

;however you can't retrieve it this way
Dim $arr = $2darr[0]; logical thinking would assume it would just pass the array as that is what is located in that spot.
A 2D array is not an array of other arrays for you to reference. Maybe some other language you have worked with (PHP?) operates that way. AutoIt arrays simply don't work that way. Period. If you must have that method, use a different language.

But it doesn't look like you spent enough time learning how AutoIt arrays DO work. For example, you can nest arrays as elements in another array:

#include <Array.au3>

Global $avOne[3][3] = [['00', '01', '02'], ['10', '11', '12'], ['20', '21', '22']]
Global $avTwo[3] = ["One", "Two", "Three"]
Global $avThree[2] = [$avOne, $avTwo]
Global $avTemp

For $n = 0 To UBound($avThree) - 1
    $avTemp = $avThree[$n]
    _ArrayDisplay($avTemp, $n)
Next

The question is what do you need to do? What are you trying to accomplish?

:)

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

I actually completed what i was working on. It just would have been nice to be able to do _arraydisplay with the nested arrays i have. also would have been nice to be able to reference the whole array easily from the one array.

It is a little program to help me put shortcuts back onto users desktops (they like to delete and move them around) without having to worry about where the shortcuts are, what they point to, if they are installed, and even if they need an icon and where the icon needs to be copied to.

#include <array.au3>
#include <GUIConstants.au3>
#include <string.au3>
#include <Misc.au3>
#include <file.au3>

Opt("TrayOnEventMode", 1)
Opt("TrayMenuMode", 1)
TrayCreateItem("Shortcuts")
TrayItemSetOnEvent(-1, "createallchk")
TrayCreateItem("")
$exititem = TrayCreateItem("Exit")
TrayItemSetOnEvent(-1, "ExitScript")
TraySetState()

Func exitscript()
    Exit
EndFunc   ;==>exitscript

Global $PCARRAY[1]
Global $checkedin[1]
Global $allchk[1]
Global $Form1_1
Global $ALL
Global $Computernames
Global $LOCALPC
Global $CANCEL
Global $clearall
Global $closeonend

createallchk()
Func _createguicheckbox()
    Global $allchk[1]
    Local $txtarr
    $var1 = 16
    $var2 = 32
    
    If Not _FileReadToArray(@WorkingDir & "\shrtcts.txt", $txtarr) Then
        MsgBox(4096, "Error", " Error reading log to Array     error:" & @error)
        Exit
    EndIf
    _ArrayDelete($txtarr, 0)
    For $i = UBound($txtarr) - 1 To 0 Step -1
        If StringLeft($txtarr[$i], 1) = '#' Then
            _ArrayDelete($txtarr, $i)
        EndIf
    Next
    For $strvar In $txtarr
        Local $strarr = StringSplit($strvar, ';')
        If UBound($strarr) > 3 Then
            _ArrayDelete($strarr, 0)
            $Checkbox = GUICtrlCreateCheckbox($strarr[2], $var1, $var2, 97, 17)
            Local $chkbxarr[1] = [$Checkbox]
            For $i = 0 To UBound($strarr) - 1
                _ArrayAdd($chkbxarr, $strarr[$i])
            Next
            If IsArray($allchk[0]) Then
                _ArrayAdd($allchk, $chkbxarr)
            Else
                $allchk[0] = $chkbxarr
            EndIf
        EndIf
        If $var2 = 208 And $var1 = 120 Then
            $var1 = 217
            $var2 = 16
        ElseIf $var1 = 16 And $var2 = 208 Then
            $var1 = 120
            $var2 = 16
        Else
            $var2 = $var2 + 16
        EndIf
    Next
EndFunc   ;==>_createguicheckbox

Func createallchk()
    Opt("GUIOnEventMode", 1)
    #Region ### START Koda GUI section ### Form=
    $Form1_1 = GUICreate("Shortcut Installer", 317, 412, 212, 130)
    GUISetOnEvent($GUI_EVENT_CLOSE, "Form1_1Close")
    GUISetOnEvent($GUI_EVENT_MINIMIZE, "Form1Minimize")
    $ALL = GUICtrlCreateCheckbox("ALL", 16, 16, 97, 17)
    GUICtrlSetOnEvent(-1, "ALLClick")
    $Computernames = GUICtrlCreateEdit("", 16, 256, 185, 89)
    $LOCALPC = GUICtrlCreateCheckbox("Local PC", 96, 232, 97, 17)
    $PUSH = GUICtrlCreateButton("PUSH", 16, 360, 75, 25, 0)
    GUICtrlSetOnEvent(-1, "PUSHClick")
    $CANCEL = GUICtrlCreateButton("CANCEL", 112, 360, 75, 25, 0)
    GUICtrlSetOnEvent(-1, "CANCELClick")
    $clearall = GUICtrlCreateCheckbox("Clear when done", 208, 352, 97, 17)
    $closeonend = GUICtrlCreateCheckbox("Close when done", 208, 376, 97, 17)
    GUISetState(@SW_SHOW)
    ;Local $arr1[4] = [$MILLENIUM, '\', '\Millennium.lnk', 'millenium']
    If Not IsAdmin() Then
        GUICtrlSetState($Computernames, $GUI_HIDE)
        GUICtrlSetState($LOCALPC, $GUI_CHECKED)
        GUICtrlSetState($closeonend, $GUI_CHECKED)
    Else
        GUICtrlCreateButton("Verify", 216, 312, 75, 25, 0)
        GUICtrlSetOnEvent(-1, "Verifyclick")
    EndIf
    _createguicheckbox()
EndFunc   ;==>createallchk

Func PUSHClick()
    Local $Computernamesvar
    Global $checkedin[1]
    For $TVAR In $allchk
        If GUICtrlRead($TVAR[0]) = $GUI_CHECKED Then
            If IsArray($checkedin[0]) Then
                _ArrayAdd($checkedin, $TVAR)
            Else
                $checkedin[0] = $TVAR
            EndIf
        EndIf
    Next
    If Not IsArray($checkedin[0]) Then
        MsgBox(0, "ERROR", "Please Select at least one item", 15)
    ElseIf GUICtrlRead($LOCALPC) = $GUI_UNCHECKED And GUICtrlRead($Computernames) = "" Then
        MsgBox(0, "ERROR", 'Please specify at least one computer. ' & GUICtrlRead($Computernames))
    Else
        _shortcutloop()
    EndIf
EndFunc   ;==>PUSHClick

Func _shortcutloop()
    $Computernamesvar = GUICtrlRead($Computernames)
    $PCARRAY = _StringSplit(StringStripWS($Computernamesvar, 7), ';|' & @CR)
    If GUICtrlRead($LOCALPC) = $GUI_CHECKED Then
        PUSHGO(@ComputerName)
    EndIf
    If Not $PCARRAY[0] = "" Then
        For $pcstr In $PCARRAY
            PUSHGO($pcstr)
        Next
    EndIf
    MsgBox(4096, "Install shortcuts", "Complete on " & UBound($PCARRAY) & " computers", 5)
    If GUICtrlRead($closeonend) = $GUI_CHECKED Then
        Exit
    ElseIf GUICtrlRead($clearall) = $GUI_CHECKED Then
        GUICtrlSetState($ALL, $GUI_UNCHECKED)
        GUICtrlSetData($Computernames, "")
        ALLClick()
    EndIf
EndFunc   ;==>_shortcutloop

Func PUSHGO($pc)
    Local $cshare
    Local $allusr
    If $pc = @ComputerName Then
        $cshare = 'c:\'
        $allusr = @DesktopCommonDir
    Else
        $cshare = '\\' & $pc & '\C$\'
        $allusr = $cshare & 'Documents and Settings\All Users\desktop'
    EndIf   
    If Not FileExists($cshare) Then
        MsgBox(4096, "C:\ Dir ", $pc & ' is offline or you do not have permission on this computer', 3)
    Else
        For $TVAR In $checkedin
            If Not FileExists($cshare & $TVAR[1]) Then
                MsgBox(4096, $TVAR[3], $TVAR[3] & " doesn't appear to be installed on " & $pc, 5)
            Else
                FileCopy(@WorkingDir & '\files' & $TVAR[2], $allusr)
            EndIf
            If UBound($TVAR) > 5 Then
                FileCopy(@WorkingDir & '\files' & $TVAR[5], $cshare & $TVAR[4])
            EndIf
        Next
    EndIf

EndFunc   ;==>PUSHGO


Func ALLClick()
    $changeallstate = GUICtrlRead($ALL)
    For $TVAR In $allchk
        GUICtrlSetState($TVAR[0], $changeallstate)
    Next
EndFunc   ;==>ALLClick
Func Verifyclick()
    For $TVAR In $allchk
        If Not FileExists(@WorkingDir & '\files' & $TVAR[2]) Then
            MsgBox(0, "Missing Shortcut", "Shortcut for " & $TVAR[3] & " is missing or corrupt.", 5)
        EndIf
    Next
    MsgBox(0, "Verify", "Verification is complete", 15)
EndFunc   ;==>Verifyclick

Func CANCELClick()
    GUIDelete()
    Exit;
EndFunc   ;==>CANCELClick
Func Form1_1Close()
    GUIDelete($Form1_1)
    Exit
EndFunc   ;==>Form1_1Close
Func Form1Minimize()
    GUIDelete($Form1_1)
EndFunc   ;==>Form1Minimize

While 1
    Sleep(10000)
WEnd
func get_quote()
   local $quote 
   switch random(1, 3, 1)
    case 1
     $quote = '"' & "A human being should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, " & _
       "design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give " & _
       "orders, cooperate, act alone, solve equations, analyze a new problem, pitch manure, program a computer, cook a tasty meal, " & _
       "fight efficiently, die gallantly. Specialization is for insects." & '"' & " Robert A. Heinlein"
    case 2
     $quote =  '"' & "Within each of us lies the power of our consent to health and sickness, to riches and poverty, to freedom " & _
       "and to slavery. It is we who control these, and not another." & '"' & " Richard Bach (Illusions)"
    case 3
     $quote ='"' & "Don't handicap your children by making their lives easy." & '"' & " Robert A. Heinlein"
   EndSwitch
   MsgBox(0, "Quote for the moment", $quote & @CRLF)
EndFunc

get_quote()
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...