[Gambas-user] Array Filtering

Dimitris Anogiatis dosida at ...626...
Sun May 24 21:10:18 CEST 2009


On Sun, May 24, 2009 at 11:04 AM, Doriano Blengino <
doriano.blengino at ...1909...> wrote:

> Dimitris Anogiatis ha scritto:
> > Hey guys,
> >
> > I was wondering if there's a way to filter an array without having to go
> > through the whole thing
> >
>


> But, the whole thing would be 4 or 5 lines of code... it is not so much...


True but if you have an array with a few thousand items then a loop is not
very efficient.


> > I've been experimenting with the Find method of the String[] but it only
> > looks for specific values
> >
> > ie:
> >
> ------------------------------------------------------------------------------------------
> > PUBLIC e AS NEW String[]
> >
> > PUBLIC SUB Form_Open()
> > e.Add("D1", 0)
> > e.Add("D9 D7", 1)
> > e.Add("D3", 2)
> > e.Add("D2", 3)
> > e.Add("D4 D2", 4)
> > END
> >
> > PUBLIC SUB Button3_Click()
> >
> > Dim d AS Integer
> >
> > d = e.Find("D2")
> > PRINT d
> >
> > END
> >
> ------------------------------------------------------------------------------------------
> > d will get the value 3. However the next value e.Find("D2) will result
> > to is a -1 (means the item's not found)
> >
> I don't understand: the first find() gives 3 (ok); the next "gives -1"
> or "should give -1"?


it should give -1 because, to my knowledge, the Find method the array object
has built-in  doesn't support wildcards and therefore I can't do something
like
 "*D2*" so I can get both "D2" & "D4 D2" as results. So I'm wondering if
there is
a way to trick the Find method to do some sort of pattern matching kinda
similar
to what LIKE does without having to resort to using a loop and checking each
array item.



> > my questions are these
> >
> > 1) is it possible to put wildcards in the string Find is looking for?
> > so for example as to also catch "D4 D2"?
> >
> I am not sure, but I think "no".
> > 2) is there a workaround so I can only keep the values Find gives
> > out a result and discard the rest of the array so essentially I get
> > only those items I want and nothing else?
> >
> It seems cloudy to me, but something like:
>
>    i =0
>    while i<e.count
>      if instr(e[i], "whatever")<>0 then break
>      inc i
>    wend
>    if i=e.count then not_found else found in e[i]
>
>
> Note that: instead of a while you can use a for-next (less lines of
> code); instead of a break you can do something and then RETURN; instead
> of instr() you can use other functions (something like match()).
>
> Cheers,


> --
> Doriano Blengino
>

it relates to performance; I'm not looking for a short way of weeding out
the right items,
but I'm trying to find the one that works the fastest in Gambas. VB6 has a
Filter function
is there anything similar to that in Gambas?


>
> "Listen twice before you speak.
> This is why we have two ears, but only one mouth."
>
>
>
> ------------------------------------------------------------------------------
> Register Now for Creativity and Technology (CaT), June 3rd, NYC. CaT
> is a gathering of tech-side developers & brand creativity professionals.
> Meet
> the minds behind Google Creative Lab, Visual Complexity, Processing, &
> iPhoneDevCamp asthey present alongside digital heavyweights like Barbarian
> Group, R/GA, & Big Spaceship. http://www.creativitycat.com
> _______________________________________________
> Gambas-user mailing list
> Gambas-user at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/gambas-user
>



More information about the User mailing list