Phaser Posted November 15, 2010 Share Posted November 15, 2010 Hi guys I am again stuck with arrays and am unsure of the whole process I should go through, if somebody could just explain whats needed that would be great. I am adding numbers to an array fine, using them fine, deleting them after using them fine but can't refill them $numarray = StringSplit($push, ","); data is text 1,2,3 etc _ArrayDelete($numarray, 0); stringsplit provides a number/amount of elements so I delete that one _ArrayPush($newarray,$numarray,1); push my numbers to a new array $thenumberarray = _ArrayUnique($newarray); get rid of duplicates _ArrayDelete($thenumberarray, 0); arrayunique provides a count so I remove it _ArrayPop($thenumberarray); for some reason I get an empty element at the end of the new array so I just pop it off All the above works fine, then I use/process the number as I need to, which works fine Then I want to empty both $newarray and $thenumberarray so I do this For $j = 0 To Ubound($thenumberarray)-1 _ArrayDelete($thenumberarray, 0) Next For $j = 0 To Ubound($newarray)-1 _ArrayDelete($newarray, 0) Next All works fine but now I go back to the start to get my next set of numbers going in but it stops working? I tried ReDim after arraydelete but got errors I must admit, I have no idea of what needs to be at the top of my script, the arrays in question have no set sizes they grow until I need to empty them, so, putting Local $newarray[?] doesn't make any sense to me, I have #include <Array.au3> Local $newarray[10],$thenumberarray,$numarray What am I doing wrong, missing, thanks anyone who helps Link to comment Share on other sites More sharing options...
Spiff59 Posted November 15, 2010 Share Posted November 15, 2010 (edited) You're misusing _ArrayPush(), it's really intended to add elements to an existing array. If you only want to copy an existing array to a new one then "$newarray = $numarray" is sufficient. Also, _ArrayUnique has an option that will return a 1-based array, eliminating the need to strip off the first element. I haven't tried it but those changes might get rid of the necessity for the call to _ArrayPop(). How you go about emptying your arrays depends upon how you plan to recreate them. Is there a need to retain the number of elements after the array is emptied? Is the array going to be redefined via a function call that creates the array, like StringSplit, or _ArrayUnique, or are you going to manually redeclare it? A quick way to zap one is just $newarray = "" (but that leaves $newarray as a simple variable, not an array, and would require redefining it as an array before referencing it). When you're using 0-based arrays (which I love) you can empty one with just "Redim $newarray[1] = [0]" to set the element count to 0. PS - I'ts much easier to read your example code if you wrap it in autoit tags or as a code snippet. Edited November 15, 2010 by Spiff59 Link to comment Share on other sites More sharing options...
Phaser Posted November 15, 2010 Author Share Posted November 15, 2010 Hi Spiff thanks for the reply I was using this bit of code to create the arrays $numarray = StringSplit($push, ","); data is text 1,2,3 etc _ArrayDelete($numarray, 0); stringsplit provides a number/amount of elements so I delete that one _ArrayPush($newarray,$numarray,1); push my numbers to a new array $thenumberarray = _ArrayUnique($newarray); get rid of duplicates _ArrayDelete($thenumberarray, 0); arrayunique provides a count so I remove it _ArrayPop($thenumberarray); for some reason I get an empty element at the end of the new array so I just pop it off This created $numarray AND $newarray, initially I only had Local $newarray[10] At the top of my page, but added the other 2 to see if it would work, it didn't All I want to do is empty them, which is working, then let that above code refill them bit by bit until they need emptying again over and over, it looks like they are emptying but not getting redefined, how do I do that? Link to comment Share on other sites More sharing options...
AdmiralAlkex Posted November 15, 2010 Share Posted November 15, 2010 When you say that you're "emptying the array" do you really mean emptying? That makes no sense, just redefine it. Also that answers your second question. Local $newarray[10] .Some of my scripts: ShiftER, Codec-Control, Resolution switcher for HTC ShiftSome of my UDFs: SDL UDF, SetDefaultDllDirectories, Converting GDI+ Bitmap/Image to SDL Surface Link to comment Share on other sites More sharing options...
Spiff59 Posted November 15, 2010 Share Posted November 15, 2010 The helpfile says that _ArrayDelete() when used on the last element sets the array to "". So, it deletes the array and you can't reference the array after that. There's not enough info for me to really give advice. In you piece of code, you get your input from a $push variable. How and when is it loaded? Is that a variable you will poll periodically to get the array data to process? If so, looping back up to that StringSplit() would reset your array regardless of whether it was empty or not. Why does it need to be manually emptied rather than overwritten? Your program flow, or process control, or input stream, your general requirement, is not apparent enough for me to make any additional suggestions. Link to comment Share on other sites More sharing options...
Phaser Posted November 16, 2010 Author Share Posted November 16, 2010 ok at the top of my page I just have Local $newarray[10] To start the whole process I have a switch statement Switch $list[0][0]; this value comes from another array, it is a number Case 0 $push = "0,42,76" Case 1 $push = "1,24,80" Case 2 $push = "2,35,85" Case Else $push = "8,121,239" EndSwitch I end up with $push $numarray = StringSplit($push, ",") _ArrayDelete($numarray, 0) _ArrayPush($newarray,$numarray,1) ArrayDisplay shows I have the correct data now, I now need to make sure there are no duplicates using arrayunique but as that adds a count to the new array[0] I get rid of that $thenumberarray = _ArrayUnique($newarray) _ArrayDelete($thenumberarray, 0) Then for some reason I get a blank item at the end of my new array so I pop it off which leaves me with exactly what I need, I continue looping through my script checking my values, if my checks fail 3 more numbers get added to the array, then maybe 3 more so the array grows to max 10, if however my checks work I would like to remove/empty/delete $numarray AND $thenumberarray so I can start on the next set of values. I have tried Redim $newarray[3] but that wont let it grow I tried Redim $newarray that keeps all the old used values and keeps adding to it rather than starting over. I hope it all makes sense as I cant think of another to explain it Link to comment Share on other sites More sharing options...
Phaser Posted November 16, 2010 Author Share Posted November 16, 2010 spiff, maybe this will help you understand I get a value 1 check it in the switch, add the 3 numbers to an array get another number 2, if that number is not in the array I add those next 3 to the array, get another number 35 FOUND in array, empty array and take 35 over to the switch and the wholeprocedure starts again. It all works as I want just that I cant empty/preper the array again afterwards? Link to comment Share on other sites More sharing options...
Phaser Posted November 17, 2010 Author Share Posted November 17, 2010 Hi Guys As it was difficult to explain I thought I would create it so you can run it to see what I want it to do, will explain details below script. expandcollapse popup#include <GUIConstantsEx.au3> #include <Array.au3> HotKeySet("{ESC}", "killme") Local $array[3] = ["0,1,2","1,2,3","2,3,4"] Local $newarray[10] $target = 4 while 1 it() WEnd Func it() $i = 0 while $i <= 2 $numarray = StringSplit($array[$i], ",") _ArrayDelete($numarray, 0) _ArrayPush($newarray,$numarray,1) $thenumberarray = _ArrayUnique($newarray) _ArrayDelete($thenumberarray, 0) _ArrayPop($thenumberarray) _ArrayDisplay($thenumberarray) ; check $thenumberarray to see if 4 is in it, if not do nothing $result = _ArraySearch($thenumberarray, $target, 0, 0, 0, 1) If $result = -1 Then MsgBox(0,"box name", "Not found yet",1);let the $newarray grow Else MsgBox(0,"box name", "Found it",1); empty the $newarray then start refilling it, same routine over and over EndIf $i = $i +1 WEnd EndFunc Func killme() Exit EndFunc As you will notice, it builds up until it finds a specific number, 4 in this case, after it finds the number is present I would like it to start from the beginning again, ie 0,1,2 should be the only numbers there, currently it starts with 0,1,2,3 I think the 3 is an actual count, not sure. I hope someone can see what I am trying to do Link to comment Share on other sites More sharing options...
MvGulik Posted November 18, 2010 Share Posted November 18, 2010 (edited) - your forgetting that $newarray is a global. (thats never cleared)- your unconditionally deleting the empty-string entry from $thenumberarray. (even if its not present.)+ You should studie the documentation, as some of the function you using have parameter flags that will return none indexed array's.+ adding 'why' remark in example code can make a big difference. (especially if you new to coding, or autoit. as it explains you thinking process in relation to the code.)Personal ... I'm still clueless about what it is you trying to do.(In general: 'code will only show what you did', not 'what you were trying to do'.)---Topic seems to have move to:Hi, my previous thread will probably confuse people even more so I thought I would put the code into a new thread so it can be run, details on what it should do below script code Edited November 18, 2010 by MvGulik "Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions.""The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014) "Believing what you know ain't so" ... Knock Knock ... 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