Phaser Posted August 28, 2009 Posted August 28, 2009 Hi everyone I am trying to find a way of adding data to an array (array push) the problem I have is I don't want to set the size [??] of the array, in php I would just set the array as, arrayname() and push data to it so it grows as much as is needed, I can't find a way to do that with autoit, any ideas? I know I can set it to something like arrayname[1000] but then I will have loads of empty values Thanks in advance
jvanegmond Posted August 28, 2009 Posted August 28, 2009 (edited) Just set the array to a size of 1, from then on ignore the first element in your loops.PHP is a little dirtier than AutoIt.Edit: The function to push into an array is _ArrayPush() (see help file). Alternative is using ReDim keyword to resize the array (_ArrayPush method does this for you). Edited August 28, 2009 by Manadar github.com/jvanegmond
Phaser Posted August 28, 2009 Author Posted August 28, 2009 Thanks Manadar but it doesn't work?? Local $list[1] _ArrayPush($list, $thedata, 1) _ArrayDisplay($list, "AFTER _ArrayPush()") only shows one entry each time, it does change with the data but only replaces the 1 element Any ideas?
jvanegmond Posted August 28, 2009 Posted August 28, 2009 Sorry, the function _ArrayPush does not resize the array. The function I had in mind was _ArrayAdd. #include <Array.au3> Dim $thedata = "IM DOING IT RONG :(" Local $list[1] _ArrayAdd($list, $thedata) _ArrayAdd($list, $thedata) _ArrayAdd($list, $thedata) _ArrayDisplay($list, "AFTER _ArrayPush()") Please provide a full ready-to-run example next time. github.com/jvanegmond
Phaser Posted August 28, 2009 Author Posted August 28, 2009 Please provide a full ready-to-run example next time. sorryok that does work, I understand ignore the first element while looping, only thing now is I need to reverse the array as the data is in the wrong direction, will use _ArrayReverse now, cheers mate
jvanegmond Posted August 28, 2009 Posted August 28, 2009 (edited) Some insights for the future, maybe. #include <Array.au3> Local $list[1] = [0] __ArrayPush($list, 1) __ArrayPush($list, 2) __ArrayPush($list, 3) _ArrayDisplay($list, "AFTER _ArrayPush()") Func __ArrayPush(ByRef $avArray, $vValue) If (Not IsArray($avArray)) Then Return SetError(1, 0, 0) If UBound($avArray, 0) <> 1 Then Return SetError(3, 0, 0) Local $iUBound = UBound($avArray) ReDim $avArray[$iUBound+1] For $i = $iUBound To 1 Step -1 $avArray[$i] = $avArray[$i - 1] Next $avArray[0] = $vValue Return 1 EndFunc ;==> __ArrayPush Edited August 28, 2009 by Manadar github.com/jvanegmond
Nutster Posted August 28, 2009 Posted August 28, 2009 Please keep in mind that the ReDim keyword copies the entire array as it resizes, so you may want to do this as infrequently as possible, otherwise this can be very slow for big arrays. I would suggest that you size the array once with your best guess of how much space you will need and then only redim if you run out of space. Use a second variable to keep track of how many elements are in use. Local $aList[99] ; estimating that I need up to 99 elements. Local $nListPos = -1 ; What is the last element I have used? -1 means that no elements are used. Local $sInput ; Used to store input values. Local $I ; Loop control variable Local $sDisplay ; Used to accumulate values to be displayed. While True $sInput = InputBox("Please enter a value.", "Testing", "", " M") If @Error = 1 Then ExitLoop For $I = 0 To $nListPos If $aList[$I] = $sInput Then ExitLoop EndIf Next If $I > $nListPos Then ; Entered string on in the array. Add it. $nListPos += 1 If $nListPos >= UBound($aList) Then ReDim $aList[$nListPos + 9] ; Grow the array, but a few at a time, to reduce the number of resizes. $aList[$nListPos] = $sInput Else ; Found the element with the entered value in the array. Remove it. If $I < $nListPos Then ; Copy the last element over this element, if not the last element. $aList[$I] = $aList[$nListPos] EndIf $nListPos -= 1 ; Decrease the active count by one. EndIf Wend $sDisplay = "" For $I = 0 To $nListPos $sDisplay &= $aList[$I] & @cr Next MsgBox(0, "Testing", $sDisplay David NuttallNuttall Computer Consulting An Aquarius born during the Age of Aquarius AutoIt allows me to re-invent the wheel so much faster. I'm off to write a wizard, a wonderful wizard of odd...
Phaser Posted August 28, 2009 Author Posted August 28, 2009 Hu Guys Thanks for the help so far, I have just encountered a new problem, I am using #include <Array.au3> Local $list[100][6] ;test to see if the array exists, it does and returns expected values If IsArray($vars) Then MsgBox(0, "array of data", $vars[3]) EndIf _ArrayPush($list, $vars); vars is an array of 6 components ; test to see if data was added If IsArray($list) Then MsgBox(0, "array of data", $list[3]) EndIf I get the error "Array variable has incorrect number of subscripts or subscript dimension range exceeded" I haven't found a solution yet, any ideas where to look, do I have it all setup correclty?
WolfWorld Posted August 28, 2009 Posted August 28, 2009 Hu Guys Thanks for the help so far, I have just encountered a new problem, I am using #include <Array.au3> Local $list[100][6] ;test to see if the array exists, it does and returns expected values If IsArray($vars) Then MsgBox(0, "array of data", $vars[3]) EndIf _ArrayPush($list, $vars); vars is an array of 6 components ; test to see if data was added If IsArray($list) Then MsgBox(0, "array of data", $list[3]) EndIf I get the error "Array variable has incorrect number of subscripts or subscript dimension range exceeded" I haven't found a solution yet, any ideas where to look, do I have it all setup correclty? Most the whole script. Main project - Eat Spaghetti - Obfuscate and Optimize your script. The most advance add-on.Website more of GadGets!
Phaser Posted August 28, 2009 Author Posted August 28, 2009 hi athiwatc, its not really possible to post the full script as theres many include files, what I really need to know is, is the way I have everything correct, this line Local $list[100][6] Is the 6 correct? I have 6 seperate variables to put in to the array, is this correct to perform that job? _ArrayPush($list, $vars) Hopefully someone can see the issue
jvanegmond Posted August 28, 2009 Posted August 28, 2009 As far as I know, _ArrayPush only handles 1D arrays. Try this: ( adapted from your code, so you'll have to "adapt" it back. ) #include <Array.au3> Local $list[100] ;test to see if the array exists, it does and returns expected values If IsArray($vars) Then MsgBox(0, "array of data", $vars[3]) EndIf _ArrayPush($list, $vars); vars is an array of 6 components ; test to see if data was added If IsArray($list) Then MsgBox(0, "array of data", $list[3]) EndIf github.com/jvanegmond
WolfWorld Posted August 28, 2009 Posted August 28, 2009 (edited) hi athiwatc, its not really possible to post the full script as theres many include files, what I really need to know is, is the way I have everything correct, this line Local $list[100][6] Is the 6 correct? I have 6 seperate variables to put in to the array, is this correct to perform that job? _ArrayPush($list, $vars) Hopefully someone can see the issue It can't push the second block (The [6]) So you will need to fix this, by using the code by @ Manadar which is above me. Edited August 28, 2009 by athiwatc Main project - Eat Spaghetti - Obfuscate and Optimize your script. The most advance add-on.Website more of GadGets!
Phaser Posted August 28, 2009 Author Posted August 28, 2009 (edited) It can't push the second block (The [6]) So you will need to fix this, I agree, but how? I only see this change Local $list[100] This doesn't help as it still wont push all the data, is there another method I can use if arraypush wont do it? From the manual for arraypush $vValue Value(s) to add (can be in an array) Edited August 28, 2009 by Phaser
jvanegmond Posted August 28, 2009 Posted August 28, 2009 Can you give us a bit more information to work with? You're currently a little bit like this: ------------------------ | = Box = | | | ? ? | | ? Nutster | Phaser | Manadar ? | | ? athiwatc ? ------------------------ github.com/jvanegmond
Phaser Posted August 28, 2009 Author Posted August 28, 2009 I'm trying my best guys, honest, if I call all variables here If IsArray($vars) Then MsgBox(0, "array of data", $vars[0] & $vars[1] & $vars[2] & $vars[3] & $vars[4] & $vars[5]) EndIf Everything shows up perfect, I just need to use that data to create the 2 D $list array
Nutster Posted August 28, 2009 Posted August 28, 2009 An array is a way to store a group of variables by using one storage name to access them. The array size is how many variables (values, etc) that you want to store. Store 5 things try,Local $aList[5]You can initialize those five, that is given them initial values.Local $aList[5] = [5, 42, 3.1415962452, "String", 0xAb1e]This array contains 5 elements, with index numbers ranging from 0 to 4. This would be the same as writing the following, but much shorter, and a little bit faster.Local $aList[5] $aList[0] = 5 $aList[1] = 42 $aList[2] = 3.1415962452 $aList[3] = "String" $aList[4] = 0xAb1eThe IsArray function just tells you whether this variable is an array. Use UBound($a, 0) to determine the number of dimensions in an array, that is the number of directions of data. A one-dimensional array can be thought of as a list. A two-dimensional array looks more like a chess board, with rows and columns. A three-dimensional array looks more like an Excel workbook, with sheets, rows and columns. You could think it like a book instead; find the page, line and word number in a particular book. 4-dimensional arrays and more dimensional arrays get hard to physically model without using quantum mechanics. Each dimension gives you more directions in which to store information.Are you sure you need to dimensions? What do the two dimensions represent? In one array I created, the first dimension was vehicle type, the second with vehicle model within the type, the third dimension was what type of component, and the fourth dimension was what measure of the part. So $a[1][0][3][2] could have represented the price (2) of the engine (3) of a sub-compact (0) car (1). This array stored a lot of data.You have one dimension already. What are you going to do with the second dimension? David NuttallNuttall Computer Consulting An Aquarius born during the Age of Aquarius AutoIt allows me to re-invent the wheel so much faster. I'm off to write a wizard, a wonderful wizard of odd...
PartyPooper Posted August 28, 2009 Posted August 28, 2009 Please keep in mind that the ReDim keyword copies the entire array as it resizes, so you may want to do this as infrequently as possible, otherwise this can be very slow for big arrays.Yes, I've found this too. I have an array with over 500,000 records and it becomes a real PITA to use _ArrayAdd with it. Haven't come up with a permanent solution as yet but I've found it quicker to dump the array to a text file, append the new data and then read it back into the array. I guess another solution may be to create a temporary array for the new data then concatenate it to the original.
Phaser Posted August 28, 2009 Author Posted August 28, 2009 hi nutster, I understand array from using php, the array I am trying to create is multi dimensional (php) (2D in Autoit I think) heres a sample of the data and process to get that data, I was asking about OCR a few weeks back but have overcome that by 2 nested whiles, take a screen capturewnd to grab the image of the number then loop through each row of pixels to find the colour I need, this all works fine and returns the number, example screenareagrab creates a barcode sort of number telling me how many green pixels per line which creates a number like 243354512 which I have already allocated to a specific number then returns $thenumber = 47 $thenumber = yellow $thenumber = square etc etc etc which I push into the array called $var, so $var contains 6 variables which i pass back and need to get them into the array called $list so i have a row of data like this [] col0 col1 col3 col4 col5 col6 [1] 47 yellow square etc etc etc [2] another set of data etc etc HTH
WolfWorld Posted August 28, 2009 Posted August 28, 2009 Can you give us a bit more information to work with? You're currently a little bit like this: ------------------------ | = Box = | | | ? ? | | ? Nutster | Phaser | Manadar ? | | ? athiwatc ? ------------------------ I find this to be funny(and of course now it's 2:21 AM right here) Main project - Eat Spaghetti - Obfuscate and Optimize your script. The most advance add-on.Website more of GadGets!
WolfWorld Posted August 28, 2009 Posted August 28, 2009 hi nutster, I understand array from using php, the array I am trying to create is multi dimensional (php) (2D in Autoit I think) heres a sample of the data and process to get that data, I was asking about OCR a few weeks back but have overcome that by 2 nested whiles, take a screen capturewnd to grab the image of the number then loop through each row of pixels to find the colour I need, this all works fine and returns the number, example screenareagrab creates a barcode sort of number telling me how many green pixels per line which creates a number like 243354512 which I have already allocated to a specific number then returns $thenumber = 47 $thenumber = yellow $thenumber = square etc etc etc which I push into the array called $var, so $var contains 6 variables which i pass back and need to get them into the array called $list so i have a row of data like this [] col0 col1 col3 col4 col5 col6 [1] 47 yellow square etc etc etc [2] another set of data etc etc HTH This is easy, give me a hour. It's al,ost 2.30 am here Main project - Eat Spaghetti - Obfuscate and Optimize your script. The most advance add-on.Website more of GadGets!
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