[Gambas-user] Array Filtering

Doriano Blengino doriano.blengino at ...1909...
Sun May 24 19:04:00 CEST 2009


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...
> 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"?
> 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

"Listen twice before you speak.
This is why we have two ears, but only one mouth."





More information about the User mailing list