amin84 Posted September 22, 2010 Share Posted September 22, 2010 Hi, The code below is very simple but yet I don't know what I'm doing wrong. I just generated an array and I want to do a task (ConsoleWrite in this case) on every item in the array. I though using a for loop will do the job but I guess not. So what am I doing wrong here? Tnx. #include <Array.au3> Global $avArray[10] $avArray[0] = "JPM" $avArray[1] = "Holger" $avArray[2] = "Jon" $avArray[3] = "Larry" $avArray[4] = "Jeremy" $avArray[5] = "Valik" $avArray[6] = "Cyberslug" $avArray[7] = "Nutster" $avArray[8] = "JdeB" $avArray[9] = "Tylo" For $i = 1 To $avArray[0] ConsoleWrite($avArray[$i]&@LF) Next Link to comment Share on other sites More sharing options...
KaFu Posted September 22, 2010 Share Posted September 22, 2010 (edited) You are assuming that the first element of the array contains the number of elements. This is not the case by default, but the value is filled by many UDFs. It's up to you, how you want to handle a certain array, if it does not contain a counter in the first element fall-back to ubound. #include <Array.au3> Global $avArray[10] $avArray[0] = 9 $avArray[1] = "Holger" $avArray[2] = "Jon" $avArray[3] = "Larry" $avArray[4] = "Jeremy" $avArray[5] = "Valik" $avArray[6] = "Cyberslug" $avArray[7] = "Nutster" $avArray[8] = "JdeB" $avArray[9] = "Tylo" For $i = 1 To $avArray[0] ConsoleWrite($avArray[$i]&@LF) Next ConsoleWrite(@crlf & @crlf) Global $avArray2[10] $avArray2[0] = "JPM" $avArray2[1] = "Holger" $avArray2[2] = "Jon" $avArray2[3] = "Larry" $avArray2[4] = "Jeremy" $avArray2[5] = "Valik" $avArray2[6] = "Cyberslug" $avArray2[7] = "Nutster" $avArray2[8] = "JdeB" $avArray2[9] = "Tylo" For $i = 0 To UBound($avArray2)-1 ConsoleWrite($avArray2[$i]&@LF) Next Edited September 22, 2010 by KaFu OS: Win10-22H2 - 64bit - German, AutoIt Version: 3.3.16.1, AutoIt Editor: SciTE, Website: https://funk.eu AMT - Auto-Movie-Thumbnailer (2022-Nov-26) BIC - Batch-Image-Cropper (2023-Apr-01) COP - Color Picker (2009-May-21) DCS - Dynamic Cursor Selector (2024-Feb-16) HMW - Hide my Windows (2018-Sep-16) HRC - HotKey Resolution Changer (2012-May-16) ICU - Icon Configuration Utility (2018-Sep-16) SMF - Search my Files (2023-Jun-03) - THE file info and duplicates search tool SSD - Set Sound Device (2017-Sep-16) Link to comment Share on other sites More sharing options...
ChrisL Posted September 22, 2010 Share Posted September 22, 2010 $array[0] was not a count of the array it was a string value so you need to use Ubound() to get the size of the array. #include <Array.au3> Global $avArray[10] $avArray[0] = "JPM" $avArray[1] = "Holger" $avArray[2] = "Jon" $avArray[3] = "Larry" $avArray[4] = "Jeremy" $avArray[5] = "Valik" $avArray[6] = "Cyberslug" $avArray[7] = "Nutster" $avArray[8] = "JdeB" $avArray[9] = "Tylo" For $i = 0 To Ubound($avArray) -1 ConsoleWrite($avArray[$i]&@LF) Next [u]Scripts[/u]Minimize gui to systray _ Fail safe source recoveryMsgbox UDF _ _procwatch() Stop your app from being closedLicensed/Trial software system _ Buffering Hotkeys_SQL.au3 ADODB.Connection _ Search 2d Arrays_SplashTextWithGraphicOn() _ Adjust Screen GammaTransparent Controls _ Eventlogs without the crap_GuiCtrlCreateFlash() _ Simple Interscript communication[u]Websites[/u]Curious Campers VW Hightops Lambert Plant Hire Link to comment Share on other sites More sharing options...
Smorg Posted September 22, 2010 Share Posted September 22, 2010 (edited) Even better, autoit kinda but not really supports something like iterators: global $avArray[10] = ["JPM", "Holger", "Jon", "Larry", "Jeremy", "Valik", "Cyberslug", "Nutster", "JdeB", "Tylo"] $data = "" for $i in $avArray consolewrite($i & @CRLF) next If autoit didn't suck, you would also be able to do something like this: for $x in [$i & @CRLF | $i <- $avArray] consolewrite($x) next or maybe: for $i in map((\x -> $x & @crlf), $avArray) consolewrite($i) next Also, question: This does not write to a DOS console unless the script is compiled as a console application..Seriously? Pain in the nuts to have to compile. No other language interpreter has a problem with this. I suppose it's easy enough with scite. I generally write in vim and run in virtualbox through samba. Edited September 22, 2010 by Smorg Link to comment Share on other sites More sharing options...
Smorg Posted September 22, 2010 Share Posted September 22, 2010 Another way (though I'm guessing autoit probably doesn't do TCO, and the implementation being a C array rather than linked list probably makes this inefficient): #include <Array.au3> global $avArray[10] = ["JPM", "Holger", "Jon", "Larry", "Jeremy", "Valik", "Cyberslug", "Nutster", "JdeB", "Tylo"] func doitmofo($x) if ubound($x) = 0 then return $x else return _ArrayPop($x) & @CRLF & doitmofo($x) endif endfunc msgbox(0, "", doitmofo($avArray)) Link to comment Share on other sites More sharing options...
amin84 Posted September 25, 2010 Author Share Posted September 25, 2010 Thanks a lot for all replies. 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