Sign in to follow this  
Followers 0
sshrum

_ArrayFilter

5 posts in this topic

#1 ·  Posted (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 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'

 

Share this post


Link to post
Share on other sites



#2 ·  Posted (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 by SolidSnake

HKTunes:Softpedia | GoogleCodeLyricToy:Softpedia | GoogleCodeRCTunes:Softpedia | GoogleCodeMichtaToolsProgrammer n. - An ingenious device that turns caffeine into code.

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

And change the If statement to...

If Not StringInStr($a1[$i],$sSubstring) Then
   _ArrayDelete($a1, $i); or way blindwig suggested
EndIf
Edited by Burrup

[u]Old Projects:[/u]A3MORGB2Hex[u]Old Functions:[/u]_TimeAdd/_TimeSub_AddComma_BubbleSort _RippleSort "He who does not understand your silence will probably not understand your words." - Elbert Hubbard.

Share this post


Link to post
Share on other sites

#5 ·  Posted (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. :evil:

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 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'

 

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0