Jump to content

Leaderboard

Popular Content

Showing content with the highest reputation on 04/07/2012 in all areas

  1. Hi all! I've spent a lot of time reading these forums and I have learned MUCH. So, thanks for that! Anyway, I decided I wanted multidimensional associative arrays for use in my scripts...and I couldn't find anything I liked. So, I wrote one! Here's the code: #include-once ; #INDEX# ======================================================================================================================= ; Title .........: xHashCollection ; AutoIt Version : 3.3.4.0 ; Language ......: English ; Description ...: Create and use Multidimentional Associative Arrays ; Author ........: OHB <me at orangehairedboy dot com> ; =============================================================================================================================== Global $_xHashCollection = ObjCreate( "Scripting.Dictionary" ), $_xHashCache ; #FUNCTION# ==================================================================================================================== ; Name...........: x ; Description ...: Gets or sets a value in an Associative Array ; Syntax.........: SET: x( $sKey , $vValue ) ; GET: x( $key ) ; Parameters ....: $sKey - the key to set or get. Examples: ; x( 'foo' ) gets value of foo ; x( 'foo.bar' ) gets value of bar which is a key of foo ; $bar = "baz" ; x( 'foo.$bar' ) gets value of baz which is a key of foo (variables are expanded) ; Return values .: Success - When setting, return the value set. When getting, returns the requested value. ; Failure - Returns a 0 ; Author ........: OHB <me at orangehairedboy dot com> ; =============================================================================================================================== Func x( $sKey = '' , $vValue = '' ) $func = "get" If @NumParams <> 1 Then $func = "set" If $sKey == '' Then If $func == "get" Then Return $_xHashCollection Else $_xHashCollection.removeAll Return '' EndIf EndIf $parts = StringSplit( $sKey , "." ) $last_key = $parts[$parts[0]] $cur = $_xHashCollection For $x = 1 To $parts[0] - 1 If Not $cur.exists( $parts[$x] ) Then If $func == "get" Then Return $cur.add( $parts[$x] , ObjCreate( "Scripting.Dictionary" ) ) EndIf $cur = $cur.item( $parts[$x] ) Next If IsPtr( $vValue ) Then $vValue = String( $vValue ) If $func == "get" Then If Not $cur.exists( $last_key ) Then Return $item = $cur.item( $last_key ) Return $item ElseIf Not $cur.exists( $last_key ) Then $cur.add( $last_key , $vValue ) Else $cur.item( $last_key ) = $vValue EndIf Return $vValue EndFunc ; #FUNCTION# ==================================================================================================================== ; Name...........: x_unset ; Description ...: Removes a key from an Associative Array ; Syntax.........: x_unset( $sKey ) ; Parameters ....: $sKey - the key to remove. ; Return values .: Success - True ; Failure - False ; Author ........: OHB <me at orangehairedboy dot com> ; =============================================================================================================================== Func x_unset( $sKey ) If $sKey == '' Then Return x( '' , '' ) $parts = StringSplit( $sKey , "." ) $cur = $_xHashCollection For $x = 1 To $parts[0] - 1 If Not $cur.exists( $parts[$x] ) Then Return False $cur = $cur.item( $parts[$x] ) Next $cur.remove( $parts[$parts[0]] ) Return True EndFunc ; #FUNCTION# ==================================================================================================================== ; Name...........: x_display ; Description ...: Displays the contents of an Associative Array ; Syntax.........: x_display( $sKey ) ; Parameters ....: $sKey - the key to display. Examples: ; x_display() displays everything ; x_display( 'foo' ) displays the contents of foo ; Author ........: OHB <me at orangehairedboy dot com> ; =============================================================================================================================== Func x_display( $key = '' ) $text = $key If $key <> '' Then $text &= " " $text &= StringTrimRight( _x_display( x( $key ) , '' ) , 2 ) $wHnd = GUICreate( "Array " & $key , 700 , 500 ) GUISetState( @SW_SHOW , $wHnd ) $block = GUICtrlCreateEdit( $text , 5 , 5 , 690 , 490 ) GUICtrlSetFont( $block , 10 , 400 , -1 , 'Courier' ) While 1 If GUIGetMsg() == -3 Then ExitLoop WEnd GUISetState( @SW_HIDE , $wHnd ) GUIDelete( $wHnd ) EndFunc ; #INTERNAL_USE_ONLY# =========================================================================================================== ; Name...........: _x_display ; Description ...: Itterates through an array and builds output for x_display ; Author ........: OHB <me at orangehairedboy dot com> ; =============================================================================================================================== Func _x_display( $item , $tab ) If IsObj( $item ) Then $text = 'Array (' & @CRLF $itemAdded = False For $i In $item $text &= $tab & " [" & $i & "] => " & _x_display( $item.item($i) , $tab & " " ) $itemAdded = True Next If Not $itemAdded Then $text &= @CRLF $text &= $tab & ')' ElseIf IsArray( $item ) Then $text = "Array" $totalItems = 1 $dimensions = UBound( $item , 0 ) For $dimension = 1 To $dimensions $size = UBound( $item , $dimension ) $totalItems *= $size $text &= "[" & $size & "]" Next $text &= " (" & @CRLF For $itemID = 0 To $totalItems - 1 $idName = '' $idNum = $itemID For $dimension = 1 To $dimensions - 1 $mul = ( $totalItems / UBound( $item , $dimension ) ) $a = Floor( $idNum / $mul ) $idName &= '[' & $a & ']' $idNum -= ( $a * $mul ) Next $idName &= '[' & $idNum & ']' $text &= $tab & " " & $idName & " => " & _x_display( Execute( "$item" & $idName ) , $tab & " " ) Next $text &= $tab & ")" Else $text = $item EndIf $text &= @CRLF Return $text EndFunc And here's how to use it: x( 'foo' , 'bar' ) ; foo = bar x( 'bar.foo' , 'baz' ) ; bar[foo] = baz MsgBox( 0 , x( 'foo' ) , x( 'bar.foo' ) ); outputs 'bar' and 'baz' The x function (chosen so it has a nice short name) takes two parameters: $key, $value. If the value is supplied, the value is set, otherwise it is retrieved. Create multi-dimensional arrays by using dot-notation. x( 'family.father.name' , 'Paul' ) is equivalent to the following PHP command: $family['father']['name'] = 'Paul'; Retrieve the value the same way! msgbox( 0 , 'Family' , 'My father's name is' & x( 'family.father.name' ) ) And, of course, you can use dynamic names using variables (provided the values don't contain dots). x( "localization.en.lang_en","English") x( "localization.es.lang_en","Ingles") For $language_code In x( 'localization' ) MsgBox( 0 , 'Language' , x( 'localization.'&$language_code&'.lang_en' ) ) Next Setting a value returns the set value, so you can do things like: x( 'foo' , x( 'bar' , 'baz' ) ) ;foo and bar both contain baz If ( x( 'query' , $query ) == '' ) MsgBox( 0 , 'Error' , 'Query cannot be blank!' ) You can use x_display to have a look at the structure. x( 'foo' , x( 'bar' , 'baz' ) ) x( 'yada' , 'yada' ) x_display() ;display everything x_display( 'foo' ) ;display just foo TODO: * Serialization / Unserialization I would appreciate feedback and suggestions! Enjoy
    1 point
  2. This is an old thread. An updated version with mouse and joystick support can be . I first wrote Reflex for a Commodore 64 in the early 80s. It started out as Centipede but quickly took on a life of its own. Back then it played to the strengths of the machine and was enjoyed by my circle of friends. I have since written it for each type of machine I have acquired and for many of the programming languages I have learned. It is my 'hello world' program Back in the day, the Vic-20, Commodore 64, TRS-80 and Apple ][ all had joysticks, which made this game much easier and more fun to play. Perhaps you'll find there is still fun to be had. I've tried to maintain the spirit of the original graphics system while taking advantage of the increased resolutions and color depth. I have no skill in graphic design so all shapes are stolen from the net and massaged into the shapes I need. I make no apologies for the lackluster graphic quality. It is still miles ahead of the blocky shapes of the original. The instruction manual. The Sound and Image Files Ver 1.4 High Score fixed, properly sorted now. Cosmetic changes to Scoreboard to accommodate more systems Reflex.au3I Ver 1.3 Arcade Style High scores feature added Ver 1.2 Fixed subscript problem if row or column was increased. Added 'Auto' option in .ini file to specify the most rows or columns that will fit. Ver 1.1 Force Field activation changed from holding down the space bar to being toggled by the space bar. Force Field no longer takes a turn to power up/down.
    1 point
  3. E1M1, Mixing native and UDF functions is usually a recipe for tears. The native function you are using to create the tabitem returns a ControlID - so use that to delete it. This works fine for me: #include <ButtonConstants.au3> #include <GUIConstantsEx.au3> #include <TabConstants.au3> #include <WindowsConstants.au3> #include <GuiTab.au3> #include <Array.au3> $Form1 = GUICreate("Use Ctrl+T and CTRL+W to add/remove tabs", 625, 443, 192, 124) $Tab1 = GUICtrlCreateTab(5, 30, 616, 406) GUICtrlSetResizing(-1, $GUI_DOCKWIDTH+$GUI_DOCKHEIGHT) GUICtrlCreateTabItem("") $Add = GUICtrlCreateButton("Add", 5, 5, 75, 25, $WS_GROUP) $Remove = GUICtrlCreateButton("Remove", 85, 5, 75, 25, $WS_GROUP) Dim $Form1_AccelTable[2][2] = [["^t", $Add],["^w", $Remove]] GUISetAccelerators($Form1_AccelTable) GUISetState(@SW_SHOW) ; Log both tabitems AND edits Global $TABS[1] Global $EDITS[1] While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Add ; Log both the tab AND the edit ControlID $TABS[UBound($TABS)-1]=GUICtrlCreateTabItem("TabSheet " & UBound($EDITS)) $EDITS[UBound($EDITS)-1]=GUICtrlCreateEdit(UBound($EDITS),7,55,610,370) ; ReDim BOTH arrays ReDim $TABS[UBound($TABS)+1] ReDim $EDITS[UBound($EDITS)+1] GUICtrlCreateTabItem("") Case $Remove ; Read the tab index $iTab = GUICtrlRead($Tab1) ; Remove the tab AND the edit from the arrays _ArrayDelete($EDITS, $iTab) _ArrayDelete($EDITS,$iTab) ; Remove the tab GUICtrlDelete($TABS[$iTab]) EndSwitch WEnd M23
    1 point
×
×
  • Create New...