jgq85 Posted July 19, 2014 Share Posted July 19, 2014 Hi I have a question regarding arrays as I'm still trying to grasp the concept. Over at http://www.autoitscript.com/wiki/Arrays, it states you have to do a -1 to get the actual index (since computer counts starting at 0) So its example: Local $arr[3] = ["element 1", "element 2", "element 3"] For $i = 0 to 3 - 1 ; We have an array with three elements but the last index is two. ConsoleWrite($arr[$i] & @LF) Next My question is, why wouldn't you just change "For $i = 0 to 3 -1" to "For $i = 0 to 2"? Link to comment Share on other sites More sharing options...
somdcomputerguy Posted July 19, 2014 Share Posted July 19, 2014 (edited) My question is, why wouldn't you just change "For $i = 0 to 3 -1" to "For $i = 0 to 2"? Well you could if you wanted. With larger arrays though, it's easier to use UBound -1, or just UBound if that's the case. Edited July 19, 2014 by somdcomputerguy - Bruce /*somdcomputerguy */ If you change the way you look at things, the things you look at change. Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted July 19, 2014 Moderators Share Posted July 19, 2014 jgq85,If you already know the size of the array, then of course you can use the actual value for the top index:Local $arr[3] = ["element 1", "element 2", "element 3"] For $i = 0 to 2 ; You can use the actual value here ConsoleWrite($arr[$i] & @LF) NextBut if you have an array of unknown size (after using FileReadToArray for example) you need to do as somdcomputerguy suggested - use UBound and subtract 1 to get the top index:Local $aLines = FileReadToArray("File_Path") For $i = 0 to UBound($aLines) - 1 ; But here you need to calculate the top index ConsoleWrite($arr[$i] & @LF) NextJust so you know, in the new Beta you get the number of lines read returned in @extended so you could use that instead. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area 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