Phaser Posted October 11, 2009 Share Posted October 11, 2009 Hi Everyone I am using a do statement to loop through an array data set x amount of times, from the array result if something is flagged I want to push it to a new array, the results could be from 1 to 25 or even 30 so the size of the new array needs to be dyanimcly created/adjusted, how do I set this up? I have tried Local $newarray[$size] $i = 0 Do $checking = _ArraySearch($list, $i,0,40,0,0,1,0) If $checking = -1 Then _ArrayPush($newarray,$i,1) $size = $size+1 EndIf $i = $i+1 Until $i = 40 _ArrayDisplay($newarray) Any help in the correct syntax would be appreciated Link to comment Share on other sites More sharing options...
Valuater Posted October 11, 2009 Share Posted October 11, 2009 (edited) UBound is what you want Do Until $i = UBound($Array_Name) -1 However, I use it this way For $x = 1 to UBound($Array) -1 ; test array here Next 8) Edited October 11, 2009 by Valuater Link to comment Share on other sites More sharing options...
Phaser Posted October 11, 2009 Author Share Posted October 11, 2009 Thnaks for the fast reply Valuater but I have not used Ubound before so am unsure of the makeup invlolved, if you could just correct the below I should be able to work with it Local $newarray[$size] $i = 0 Do $checking = _ArraySearch($list, $i,0,40,0,0,1,0) If $checking = -1 Then _ArrayPush($newarray,$i,1) $size = $size+1 EndIf $i = $i+1 Until $i = 40 <---------------does Ubound replace this _ArrayDisplay($newarray) Link to comment Share on other sites More sharing options...
jebus495 Posted October 11, 2009 Share Posted October 11, 2009 (edited) Thnaks for the fast reply Valuater but I have not used Ubound before so am unsure of the makeup invlolved, if you could just correct the below I should be able to work with it Local $newarray[$size] $i = 0 Do $checking = _ArraySearch($list, $i,0,40,0,0,1,0) If $checking = -1 Then _ArrayPush($newarray,$i,1) $size = $size+1 EndIf $i = $i+1 Until $i = 40 <---------------does Ubound replace this _ArrayDisplay($newarray) Yeah. UBound($Array_Name) -1 Replaces 40 But like he said: For $x = 1 to UBound($Array) -1 ; test array here Next Should probably be the loop to use for this particular function. Looks nicer and it's a bit easier to understand. Edited October 11, 2009 by jebus495 Link to comment Share on other sites More sharing options...
Phaser Posted October 11, 2009 Author Share Posted October 11, 2009 (edited) Well it doesn't work I get an error here Local $newarray[$size] Local $newarray[^ ERROR EDIT And, the 40 cant be changed as its the figure I use the create what to search the array for, the array I am attempting to dynamicly populate is this bit If $checking = -1 Then _ArrayPush($newarray,$i,1) $size = $size+1 EndIf Please advise Edited October 11, 2009 by Phaser Link to comment Share on other sites More sharing options...
PsaltyDS Posted October 11, 2009 Share Posted October 11, 2009 Well it doesn't work I get an error here Local $newarray[$size] Local $newarray[^ ERROR EDIT And, the 40 cant be changed as its the figure I use the create what to search the array for, the array I am attempting to dynamicly populate is this bit If $checking = -1 Then _ArrayPush($newarray,$i,1) $size = $size+1 EndIf Please advise And where are you setting $size to an integer value before using it? Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law Link to comment Share on other sites More sharing options...
Authenticity Posted October 11, 2009 Share Posted October 11, 2009 this kind of dynamic array is suited more to a counter checked array. In other words, you keep the first element as the number of the next available index into the array and redim the array as necessary. For example: Local $newarray[20] = [1] ; Will be reallocated as necessary. For $i = 0 To UBound($list)-1 If _ArraySearch($list, $i,0,40,0,0,1,0) = -1 Then If $newarray[0] >= UBound($newarray) Then Redim $newarray[$newarray[0]+10] ; + 10 elements to avoid redimming over and over which is slower. $newarray[$newarray[0]] = $i $newarray[0] += 1 EndIf Next ; Redim to the number of elements in the array. Redim $newarray[$newarray[0]-1] ; Now reverse the array because you've appended elements instead of pushing them. I hope it's correct because it's not tested. PS. PsaltyDS is right. AutoIt should throw a warning that $size is used but never assigned a value. Link to comment Share on other sites More sharing options...
monoceres Posted October 11, 2009 Share Posted October 11, 2009 PS. PsaltyDS is right. AU3Check should throw a warning that $size is used but never assigned a value.Nice to see you btw, you've been away for a while Broken link? PM me and I'll send you the file! Link to comment Share on other sites More sharing options...
Phaser Posted October 12, 2009 Author Share Posted October 12, 2009 Thanks for the replies people, there is a bit of a misunderstanding, $list is the array I am checking against and the number 40 is the actual values I am looking for within $list Local $newarray[20] = [1] ; Will be reallocated as necessary. For $i = 0 To 40 If _ArraySearch($list, $i,0,40,0,0,1,0) = -1 Then If $newarray[0] >= UBound($newarray) Then Redim $newarray[$newarray[0]+10] ; + 10 elements to avoid redimming over and over which is slower. $newarray[$newarray[0]] = $i $newarray[0] += 1 EndIf Next ; Redim to the number of elements in the array. Redim $newarray[$newarray[0]-1] It almost works perfectly BUT I am getting one duplicate entry when I display the array always at [0] As I am looping through from 0 to 40 to see if that value is in $list, if not in add to newarray 0 to 40 is only checking each value once so how does it end up with a repeat as I said at the first position of the redim array [0]? how do I stop this? Link to comment Share on other sites More sharing options...
Authenticity Posted October 12, 2009 Share Posted October 12, 2009 It's the array counter. It's used in the loop as you can see. Use _ArrayDelete() if you want, it'll make _ArrayDisplay prettier but it has a good context in consequence loops: For $i = 1 To $newarray[0] ; Work with array. Next If it's not the behavior you want, use _ArrayDelete(). Link to comment Share on other sites More sharing options...
Phaser Posted October 12, 2009 Author Share Posted October 12, 2009 thanks Authenticity I have used _ArrayDelete() I am now using the values from the newarray to create a controlclick command, I have an include file with the x/y positions like this $39x = 200 $39y = 310 and am trying to use just one controlclick command in a loop, how do I format the variable, for example $xval = "$" & $newarray[$x] & "x" <------------- Ideally this should produce $39x $yval = "$" & $newarray[$x] & "y" <------------- Ideally this should produce $39y, they do but are not working when transferred to the controlclick command ControlClick("WorkForce", "", "", "left", 1, $xval, $yval) I have the loop and everything working fine, tested with msgbox popup, it's the $sign thats causing problems but needs to be there so it can scan the include for the x/y coords Link to comment Share on other sites More sharing options...
water Posted October 12, 2009 Share Posted October 12, 2009 (edited) In the ControlClick command you don't want the value of $xval and $yval (which is "$39x" and "$39y") but the value of the variables defined by $xval and $yval. So you have to evaluate them. See the Eval function $xval = Eval("{:content:}quot; & $newarray[$x] & "x")should give you the value of $s39x in $xval. Edited October 12, 2009 by water My UDFs and Tutorials: Spoiler UDFs:Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki  Link to comment Share on other sites More sharing options...
Phaser Posted October 12, 2009 Author Share Posted October 12, 2009 (edited) thanks for the help water but it gives me an error Unterminated string.: $xval = Eval("{:content:}quot; & $newarray[$x] & "x") I also tried $xval = Eval("$" & $newarray[$x] & "x") But that didnt produce a result/click Edited October 12, 2009 by Phaser Link to comment Share on other sites More sharing options...
water Posted October 12, 2009 Share Posted October 12, 2009 (edited) My fault. You have to omit the "$" as Eval already knows that a variable is to be used.$xval = Eval($newarray[$x] & "x") Edited October 12, 2009 by water My UDFs and Tutorials: Spoiler UDFs:Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki  Link to comment Share on other sites More sharing options...
Phaser Posted October 12, 2009 Author Share Posted October 12, 2009 many thanks it now works as expected, cheers water Link to comment Share on other sites More sharing options...
Phaser Posted October 12, 2009 Author Share Posted October 12, 2009 Hit a problem, the array fills fine, everything runs fine after I manipulate the data thats in the array, I now need to empty the array so it can be refilled again with new valyues, whats the best method, I have tried do, while and for with _ArrayDelete like this $howbig = UBound($newarray) ; get size of array $j = 0 While $j <= $howbig _ArrayDelete($newarray, $j) $j = $j+1 WEnd Its not working, any ideas Link to comment Share on other sites More sharing options...
Authenticity Posted October 12, 2009 Share Posted October 12, 2009 (edited) $newarray = 0 Local $newarray[20] ConsoleWrite(UBound($newarray) & @CRLF)..for exampleEdit: By the way, read the function description of a function before you use it. _ArrayDelete() remove the element from the array so the new array is resized or "" if it's upper bound was 1. Edited October 12, 2009 by Authenticity Link to comment Share on other sites More sharing options...
Phaser Posted October 12, 2009 Author Share Posted October 12, 2009 I did read its description, thats why I thought I could just cycle through the array removing each element at the end of the script, I have no idea what you meant with this $newarray = 0 Local $newarray[20] ConsoleWrite(UBound($newarray) & @CRLF) I understand concolewrite, why would I want to do it? Link to comment Share on other sites More sharing options...
Authenticity Posted October 12, 2009 Share Posted October 12, 2009 Well, if you remove item from an array using _ArrayDelete() then you shouldn't increase the counter or delete the array elements using an inverse loop, i.e: For $j = UBound($newarray)-1 To 0 Step -1 ; Delete element Next ; Or just simple as this loop For $j = 0 To Ubound($newarray)-1 _ArrayDelete($newarray, 0) ; Always the first element until $newarray = "" or nothing. Next The ConsoleWrite is there for demonstration nothing more. When you set an array variable to 0 it's array attribute is negated but so you can't use ReDim (not tested) but it can be redeclared as one. Link to comment Share on other sites More sharing options...
GEOSoft Posted October 12, 2009 Share Posted October 12, 2009 Another method is to copy the array and loop through the new one but perform the actions on the original. _ArrayDisplay($aArray) $aTemp = $aArray For $i = 0 To Ubound($aTemp) If $aTemp[$i] = "" Then _ArrayDelete($aArray, $i) Next _ArrayDisplay($aArray) George Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.*** The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number. Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else. "Old age and treachery will always overcome youth and skill!" 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