Finding a value within an array

Post

Posted
Rating:
#1 (In Topic #1000)
Regular
PartierSP is in the usergroup ‘Regular’
If I had an array with various integer values and wanted to find what index value contains a given search value, is there a command to do that, or do I simply have to write a little search function?

For example.  If I had the following array:

Code (gambas)

  1. ar=array(5,6,4,7,3,8,2,9,1,0)
  2.  
and I was looking for 3, I would want a function to return the value 4.
Or, if I was looking for 6, I'd want it to return the value 1.

Is there a built in function that can do this, or do I have to write a simple FOR EACH loop and do it manually.  I know in some languages like PHP they have such functions array_search($needle, $haystack).
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Guru
cogier is in the usergroup ‘Guru’
There is an easy way to do this: -

Code (gambas)

  1. Public Sub Form_Open()
  2.  
  3.   Dim ar As Integer[] = [5, 6, 4, 7, 3, 8, 2, 9, 1, 0]
  4.  
  5.   Print ar.Find(3)  ''Result = 4
  6.   Print ar.Find(6)  ''Result = 1
  7.  

There is a lot you can do with arrays. Have a look ]here.
Online now: No Back to the top

Post

Posted
Rating:
#3
Regular
PartierSP is in the usergroup ‘Regular’
 Perfect!  That's exactly what I was looking for.  Just I'm kinda surprised that it was listed in the help under Integer[]  and not under Array.  No wonder I didn't find it.
Online now: No Back to the top

Post

Posted
Rating:
#4
Avatar
Regular
thatbruce is in the usergroup ‘Regular’
 Because not all Arrays have searchable contents, for example Object[]

Online now: No Back to the top
1 guest and 0 members have just viewed this.