PartyPooper Posted July 9, 2011 Share Posted July 9, 2011 I was wondering if there was a simple way to increment an array using addition assignment (ie. +=) I know you can use $aArray[$n + 1] but how do I go about using $aArray[$n += 1] without throwing an error? Essentially, I want something like this: $aArray[$n += 1] = "Item 1" $aArray[$n += 1] = "Item 2" $aArray[$n += 1] = "Item 3" ... I guess I could have: $n += 1 $aArray[$n] = "Item 1" $n += 1 $aArray[$n] = "Item 2" $n += 1 $aArray[$n] = "Item 3" ... but that's a little long-winded so I'm wondering if there's some quicker/easier method. Link to comment Share on other sites More sharing options...
AdmiralAlkex Posted July 9, 2011 Share Posted July 9, 2011 (edited) Are you saying that you want to resize the array (ReDim) or a For-loop without the For-loop (just weird)? Why don't you use a For-loop? Global $avArray[3]=["first", "second", "third"] For $iX = 0 To UBound($avArray) -1 ConsoleWrite($avArray[$iX] & @CRLF) Next Edit: Or you are saying StringSplit()? Global $sSomething = "first|second|third" Global $avArray = StringSplit($sSomething, "|") For $iX = 1 To $avArray[0] ConsoleWrite($avArray[$iX] & @CRLF) Next Edited July 9, 2011 by AdmiralAlkex .Some of my scripts: ShiftER, Codec-Control, Resolution switcher for HTC ShiftSome of my UDFs: SDL UDF, SetDefaultDllDirectories, Converting GDI+ Bitmap/Image to SDL Surface Link to comment Share on other sites More sharing options...
jaberwacky Posted July 9, 2011 Share Posted July 9, 2011 (edited) Global $aArray[3] Global $n = -1 $aArray[increment($n)] = "Item 1" $aArray[increment($n)] = "Item 2" $aArray[increment($n)] = "Item 3" Func increment(ByRef $var) $var += 1 Return $var EndFunc Better yet: Global $aArray[3] $aArray[increment()] = "Item 1" $aArray[increment()] = "Item 2" $aArray[increment()] = "Item 3" Func increment() Local Static $var = -1 $var += 1 Return $var EndFunc Edited July 10, 2011 by LaCastiglione Helpful Posts and Websites: AutoIt3 Variables and Function Parameters MHz | AutoIt Wiki | Using the GUIToolTip UDF BrewManNH | Can't find what you're looking for on the Forum? Link to comment Share on other sites More sharing options...
PartyPooper Posted July 9, 2011 Author Share Posted July 9, 2011 Thanks guys, both answers solve the problem in slightly different ways which is great. Now I just have to make up my mind which would be easier for me in the long run Link to comment Share on other sites More sharing options...
jaberwacky Posted July 9, 2011 Share Posted July 9, 2011 Probably Admiral 'Man Hair' Alkex's solutions are the most maintainable. Helpful Posts and Websites: AutoIt3 Variables and Function Parameters MHz | AutoIt Wiki | Using the GUIToolTip UDF BrewManNH | Can't find what you're looking for on the Forum? Link to comment Share on other sites More sharing options...
AdmiralAlkex Posted July 9, 2011 Share Posted July 9, 2011 Probably Admiral 'Man Hair' Alkex's solutions are the most maintainable.OMG, I'm sooo changing my name to that! .Some of my scripts: ShiftER, Codec-Control, Resolution switcher for HTC ShiftSome of my UDFs: SDL UDF, SetDefaultDllDirectories, Converting GDI+ Bitmap/Image to SDL Surface Link to comment Share on other sites More sharing options...
wolf9228 Posted July 10, 2011 Share Posted July 10, 2011 I was wondering if there was a simple way to increment an array using addition assignment (ie. +=) I know you can use $aArray[$n + 1] but how do I go about using $aArray[$n += 1] without throwing an error? Essentially, I want something like this: $aArray[$n += 1] = "Item 1" $aArray[$n += 1] = "Item 2" $aArray[$n += 1] = "Item 3" ... I guess I could have: $n += 1 $aArray[$n] = "Item 1" $n += 1 $aArray[$n] = "Item 2" $n += 1 $aArray[$n] = "Item 3" ... but that's a little long-winded so I'm wondering if there's some quicker/easier method. #include <Array.au3> Dim $YArray1D[1] $YArray1D[0] = 1 AddNewElement($YArray1D,2) AddNewElement($YArray1D,3) AddNewElement($YArray1D,4) AddNewElement($YArray1D,5) AddNewElement($YArray1D,6) AddNewElement($YArray1D,7) AddNewElement($YArray1D,8) AddNewElement($YArray1D,9) AddNewElement($YArray1D,10) _ArrayDisplay($YArray1D,"YArray1D") Dim $XArray1D[1] AddNewElement($XArray1D) AddNewElement($XArray1D) AddNewElement($XArray1D) AddNewElement($XArray1D) AddNewElement($XArray1D) AddNewElement($XArray1D) AddNewElement($XArray1D) AddNewElement($XArray1D) AddNewElement($XArray1D) _ArrayDisplay($XArray1D,"XArray1D") Func AddNewElement(ByRef $aArray1D,$value = "") if Not IsArray($aArray1D) Or UBound($aArray1D, 0) = 2 Then Return SetError(1,0,0) $rows = UBound($aArray1D) ReDim $aArray1D[$rows + 1] $aArray1D[$rows] = $value Return SetError(0,0,$rows) EndFunc صرح السماء كان هنا Link to comment Share on other sites More sharing options...
wolf9228 Posted July 10, 2011 Share Posted July 10, 2011 Or expandcollapse popup#include <Array.au3> Dim $YArray1D = 0 AddNewElement($YArray1D,1) AddNewElement($YArray1D,2) AddNewElement($YArray1D,3) AddNewElement($YArray1D,4) AddNewElement($YArray1D,5) AddNewElement($YArray1D,6) AddNewElement($YArray1D,7) AddNewElement($YArray1D,8) AddNewElement($YArray1D,9) AddNewElement($YArray1D,10) _ArrayDisplay($YArray1D,"YArray1D") Dim $XArray1D = 0 AddNewElement($XArray1D) AddNewElement($XArray1D) AddNewElement($XArray1D) AddNewElement($XArray1D) AddNewElement($XArray1D) AddNewElement($XArray1D) AddNewElement($XArray1D) AddNewElement($XArray1D) AddNewElement($XArray1D) AddNewElement($XArray1D) _ArrayDisplay($XArray1D,"XArray1D") Func AddNewElement(ByRef $aArray1D,$value = "") if UBound($aArray1D, 0) = 2 Then Return SetError(1,0,0) if Not IsArray($aArray1D) Then Dim $TempArray[1] $aArray1D = $TempArray $aArray1D[0] = $value Return SetError(0,0,0) Else $rows = UBound($aArray1D) ReDim $aArray1D[$rows + 1] $aArray1D[$rows] = $value Return SetError(0,0,$rows) EndIf EndFunc صرح السماء كان هنا Link to comment Share on other sites More sharing options...
jaberwacky Posted July 11, 2011 Share Posted July 11, 2011 (edited) Yeah? Well how about this? expandcollapse popup#include <Array.au3> #include "AutoItObject.au3" _AutoItObject_StartUp() Global $array = Array() $array.Add("Item 1") $array.Add("Item 2") $array.Add("Item 3") $array.Display() Func array() Local $this = _AutoItObject_Class() $this.AddProperty("Array", $elscope_private, '') $this.AddProperty("Size", $elscope_private, 0) $this.AddMethod("Grow", "array_grow", True) $this.AddMethod("Add", "array_add") $this.AddMethod("Display", "array_display") $this.AddMethod("GetSize", "array_get_size") Return $this.Object EndFunc Func array_add($this, Const $item) $this.Grow() Local $array = $this.Array $array[$this.Size - 1] = $item $this.Array = $array EndFunc Func array_grow($this) Local $array = $this.Array If $this.Size = 0 Then Dim $array[1] Else ReDim $array[$this.Size + 1] EndIf $this.Size = $this.Size + 1 $this.Array = $array EndFunc Func array_display($this) Local $array = $this.Array Local Const $size = $this.Size - 1 For $i = 0 To $size ConsoleWrite("$array: " & $array[$i] & @LF) Next EndFunc Edited July 11, 2011 by LaCastiglione Helpful Posts and Websites: AutoIt3 Variables and Function Parameters MHz | AutoIt Wiki | Using the GUIToolTip UDF BrewManNH | Can't find what you're looking for on the Forum? Link to comment Share on other sites More sharing options...
AdmiralAlkex Posted July 11, 2011 Share Posted July 11, 2011 @LaCastiglione Redim +1 will be horribly slow, you should Redim *2 .Some of my scripts: ShiftER, Codec-Control, Resolution switcher for HTC ShiftSome of my UDFs: SDL UDF, SetDefaultDllDirectories, Converting GDI+ Bitmap/Image to SDL Surface Link to comment Share on other sites More sharing options...
guinness Posted July 11, 2011 Share Posted July 11, 2011 This is how I use ReDim - UDF List: _AdapterConnections() • _AlwaysRun() • _AppMon() • _AppMonEx() • _ArrayFilter/_ArrayReduce • _BinaryBin() • _CheckMsgBox() • _CmdLineRaw() • _ContextMenu() • _ConvertLHWebColor()/_ConvertSHWebColor() • _DesktopDimensions() • _DisplayPassword() • _DotNet_Load()/_DotNet_Unload() • _Fibonacci() • _FileCompare() • _FileCompareContents() • _FileNameByHandle() • _FilePrefix/SRE() • _FindInFile() • _GetBackgroundColor()/_SetBackgroundColor() • _GetConrolID() • _GetCtrlClass() • _GetDirectoryFormat() • _GetDriveMediaType() • _GetFilename()/_GetFilenameExt() • _GetHardwareID() • _GetIP() • _GetIP_Country() • _GetOSLanguage() • _GetSavedSource() • _GetStringSize() • _GetSystemPaths() • _GetURLImage() • _GIFImage() • _GoogleWeather() • _GUICtrlCreateGroup() • _GUICtrlListBox_CreateArray() • _GUICtrlListView_CreateArray() • _GUICtrlListView_SaveCSV() • _GUICtrlListView_SaveHTML() • _GUICtrlListView_SaveTxt() • _GUICtrlListView_SaveXML() • _GUICtrlMenu_Recent() • _GUICtrlMenu_SetItemImage() • _GUICtrlTreeView_CreateArray() • _GUIDisable() • _GUIImageList_SetIconFromHandle() • _GUIRegisterMsg() • _GUISetIcon() • _Icon_Clear()/_Icon_Set() • _IdleTime() • _InetGet() • _InetGetGUI() • _InetGetProgress() • _IPDetails() • _IsFileOlder() • _IsGUID() • _IsHex() • _IsPalindrome() • _IsRegKey() • _IsStringRegExp() • _IsSystemDrive() • _IsUPX() • _IsValidType() • _IsWebColor() • _Language() • _Log() • _MicrosoftInternetConnectivity() • _MSDNDataType() • _PathFull/GetRelative/Split() • _PathSplitEx() • _PrintFromArray() • _ProgressSetMarquee() • _ReDim() • _RockPaperScissors()/_RockPaperScissorsLizardSpock() • _ScrollingCredits • _SelfDelete() • _SelfRename() • _SelfUpdate() • _SendTo() • _ShellAll() • _ShellFile() • _ShellFolder() • _SingletonHWID() • _SingletonPID() • _Startup() • _StringCompact() • _StringIsValid() • _StringRegExpMetaCharacters() • _StringReplaceWholeWord() • _StringStripChars() • _Temperature() • _TrialPeriod() • _UKToUSDate()/_USToUKDate() • _WinAPI_Create_CTL_CODE() • _WinAPI_CreateGUID() • _WMIDateStringToDate()/_DateToWMIDateString() • Au3 script parsing • AutoIt Search • AutoIt3 Portable • AutoIt3WrapperToPragma • AutoItWinGetTitle()/AutoItWinSetTitle() • Coding • DirToHTML5 • FileInstallr • FileReadLastChars() • GeoIP database • GUI - Only Close Button • GUI Examples • GUICtrlDeleteImage() • GUICtrlGetBkColor() • GUICtrlGetStyle() • GUIEvents • GUIGetBkColor() • Int_Parse() & Int_TryParse() • IsISBN() • LockFile() • Mapping CtrlIDs • OOP in AutoIt • ParseHeadersToSciTE() • PasswordValid • PasteBin • Posts Per Day • PreExpand • Protect Globals • Queue() • Resource Update • ResourcesEx • SciTE Jump • Settings INI • SHELLHOOK • Shunting-Yard • Signature Creator • Stack() • Stopwatch() • StringAddLF()/StringStripLF() • StringEOLToCRLF() • VSCROLL • WM_COPYDATA • More Examples... Updated: 22/04/2018 Link to comment Share on other sites More sharing options...
PartyPooper Posted July 14, 2011 Author Share Posted July 14, 2011 Actually in this case, LaCastiglione's first post was the one I ended up going with as it made more sense and was easier/quicker to code up. Cheers for that. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now