sshrum Posted June 21, 2005 Share Posted June 21, 2005 (edited) Func _ArrayFilter($a1, $sSubstring, $i_base = 1) For $i = UBound($a1) - 1 To $i_base Step - 1 If stringinStr($a1[$i],$sSubstring) Then Continueloop Else _ArrayDelete($a1, $i) EndIf Next $a1[0] = UBound($a1) - 1 return $a1 EndFunc ;==>_ArrayFilter Edited June 21, 2005 by sshrum Sean Shrum :: http://www.shrum.net All my published AU3-based apps and utilities 'Make it idiot-proof, and someone will make a better idiot' Link to comment Share on other sites More sharing options...
FuryCell Posted June 22, 2005 Share Posted June 22, 2005 (edited) Cool. This would work nicely to filter filetypes out of the array returned by my filelist function. Also has many other uses. P.S. You should make all your variables local. the reason for this is becuase if somebody has already decared $I your function would change it. By using local this problem can be eliminated. Edited June 22, 2005 by SolidSnake HKTunes:Softpedia | GoogleCodeLyricToy:Softpedia | GoogleCodeRCTunes:Softpedia | GoogleCodeMichtaToolsProgrammer n. - An ingenious device that turns caffeine into code. Link to comment Share on other sites More sharing options...
blindwig Posted June 22, 2005 Share Posted June 22, 2005 I'm going to have to tell you the same thing I've said about your other array functions released today: You are not even testing your functions before you post them. I can tell just by looking at this function that it won't work correctly. If the array is 1-based, you're still using Ubound - 1 to find the top. If the array is 0-based, you're still destroying the first element. You don't do any range-check on the $i_base - I could put -1 or 10000 in there and your function would crash or give an irradic result. Also, is it necessary to run _ArrayDelete so many times? It's very slow and memory intensive, since it makes a copy of the first array each time it is run. Wouldn't it be much better to build a second array from the elements that matched the filter, and then return that? My UDF Threads:Pseudo-Hash: Binary Trees, Flat TablesFiles: Filter by Attribute, Tree List, Recursive Find, Recursive Folders Size, exported to XMLArrays: Nested, Pull Common Elements, Display 2dSystem: Expand Environment Strings, List Drives, List USB DrivesMisc: Multi-Layer Progress Bars, Binary FlagsStrings: Find Char(s) in String, Find String in SetOther UDF Threads I Participated:Base64 Conversions Link to comment Share on other sites More sharing options...
buzz44 Posted June 22, 2005 Share Posted June 22, 2005 (edited) And change the If statement to... If Not StringInStr($a1[$i],$sSubstring) Then _ArrayDelete($a1, $i); or way blindwig suggested EndIf Edited June 22, 2005 by Burrup qq Link to comment Share on other sites More sharing options...
sshrum Posted June 22, 2005 Author Share Posted June 22, 2005 (edited) You are not even testing your functions before you post them. I can tell just by looking at this function that it won't work correctly.Yes, I am new to coding with AutoIT and I do not claim to be a professional programmer but I did test this with i_base values of 0 and 1 and with several different arrays. For my scripts, this works fine as it conforms to the array structs that come out of the array UDFs. Granted users can create their own arrays but to date, my limited exposure has been with the UDFs. Yes, passing invalid i_base values will choke the funct but that's caller error. The func does what it implies and works if given valid input.Garbage in...yada yada yada. Id10t checking can be added last after the major kinks are out (although it doesn't hurt to put some sort of value checking in there).As you pointed out, code optimization is not my forte (yet), hence the post (I gotta learn somewhere). Seemed like it would be quicker to delete the elements out. Granted, I haven't done time trials on this so I've no ground to stand on in this respect. Always open to suggestions and critiques.If however you are saying that this sort of input is unwelcome, I can always just keep these sorts of things to myself and stumble along. Edited June 22, 2005 by sshrum Sean Shrum :: http://www.shrum.net All my published AU3-based apps and utilities 'Make it idiot-proof, and someone will make a better idiot' 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