Jump to content
Sign in to follow this  
PartyPooper

Is there a way to increment an array using addition?

Recommended Posts

PartyPooper

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.

Share this post


Link to post
Share on other sites
AdmiralAlkex

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 by AdmiralAlkex

Share this post


Link to post
Share on other sites
jaberwacky

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 by LaCastiglione

Share this post


Link to post
Share on other sites
PartyPooper

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 :)

Share this post


Link to post
Share on other sites
wolf9228

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

صرح السماء كان هنا

 

Share this post


Link to post
Share on other sites
wolf9228

Or

#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

صرح السماء كان هنا

 

Share this post


Link to post
Share on other sites
jaberwacky

Yeah? Well how about this?

#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 by LaCastiglione

Share this post


Link to post
Share on other sites
guinness

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 parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Share this post


Link to post
Share on other sites
PartyPooper

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.

Share this post


Link to post
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
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.