Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Searching an array and returning the position

Status
Not open for further replies.

stress

Technical User
Oct 5, 2000
1
US
Hi, I thought maybe you might be
able to help me. Here goes......

I need help in setting up an array ranging from 1,000 -
5,000 but the array only holds 1,000 random numbers.
Then I need to be able to search for a number in the
array and have it tell me its position in the array. If not found
it needs to return a value of -1 and if found it needs
to display the info.

Any help is greatly appreciated.....thanks! [sig][/sig]
 
The only way I know (there may be another, better way) to do this is to loop through the array with a counter checking each entry:

Private Function Get_Data_Position(ByVal lData As Long) As Long

Dim bFound As Boolean
Dim lPosition As Long

' assume lData holds what you are looking for
' assume lArray is an array of random longs
lPosition = -1
Do Until bFound Or lPosition >= UBound(lArray)
lPosition = lPosition + 1
If lData = lArray(lPosition) Then
bFound = True
End If
Loop
If bFound Then
Get_Data_Position = lPosition
Else
Get_Data_Position = -1
End If

End Function

call the function passing in the number to look for (as long).
This assumes that the array is visible to the function.

Simon [sig][/sig]
 
A different approach - would depend on 'stress's' ability.

Instead of using an array, create a db/table in code. Store the values in the table. Use Seek/Find to get the value. User the record number to return the position.

Also, if the information is already in the array for pther reasons, it may be more effort in redesign than the benefit.

Probably a BAD idea. Stress's profile doesn't tell enough to SWAG wheather he could do this w/o A LOT of coaching.


[sig]<p>MichaelRed<br><a href=mailto:mred@duvallgroup.com>mred@duvallgroup.com</a><br>There is never time to do it right but there is always time to do it over[/sig]
 
I'm new to this...hope I'm doing this right. Can't help you with the needed 4000 places in an array that only holds 1000 (I'm not sure I understand that point anyway...why would the array be limited to 1000 values???).
Anyway, with regard to finding the position (index) of a specific value in the array:
Sub blahblahblah()
Dim Value_1 as <whatever>
Dim x as integer, y as integer
Dim array_1(1000 to 5000)
For x=1000 to 5000
If value_1=array_1(x) then
y=x
exit for
end if
next x
If y=0 then y=-1
end sub
Hope that helps. :) [sig][/sig]
 
Sparse array approach.

Private Type MyArrayType
[tab]ArayIndx as Integer
[tab]ArayVal as Integer
End Type
Dim MyArray(999) as MyArrayType

'This gives us 1K items to use

'Other Code - Populate this array with the &quot;Index Location&quot; and &quot;value&quot;. Value is hte same as would be in the array of 1000 to 5000. The index is just where - in the other array - this value would be placed.

'Now, search [MyArray.ArayVal] for the desired number - and return [MyAray.ArayIndx]

Function SrchVal(ValIn as Integer) As Integer

Dim Idx as Integer

For Idx = 0 to UBound(MyAray)
[tab]If (MyAray(Idx).ArayVal = ValIn) Then
[tab][tab]SrchVal = MyAray(Idx).ArayIndx
[tab][tab]ExitFor
[tab]End If

If (SrchVal = 0) Then
[tab] SrchVal = -1
End If

Exit Function

Not really much help, but it could reduce the search by ~ 3/4? [sig]<p>MichaelRed<br><a href=mailto:mred@duvallgroup.com>mred@duvallgroup.com</a><br>There is never time to do it right but there is always time to do it over[/sig]
 
I hope I'm not out of line the way I am posting this reply, I'm new to this forum.
This was a very interesting problem, and as I too am in the learning process with VB it took some time for me to come up with a response.
Anyway for what it may be worth, here goes:

Below is an image of the form I used in developing this program.

(sorry the reply window would not accept my image. If you would like I can e-meil the image to you. My e-mail is rgyeend@comsys.net

The three display areas above the build buttons are actually label controls with properties set to resemble TextBox controls, which have nothing to do with the operation of this program, but rather act as activity displays.
The first window will show 500500 which is the sum of the digits from 1 to 1000. The third window will show the same.
The second window will show the sum of the array indexes of the search array as selected by the second array.
There are three arrays in the program, the first develops the group of random numbers from 1 to 1000.
The second array selects another group of 1000 random numbers in the range of 1000 to 5000. These numbers will determine which position in the third array the first group of numbers will be placed.
The lower left display window (real TextBox) is for entering the number you wish to find. Enter a number between 1 and 1000 and click the search button. The right window will show the number searched for and the array index number of the third array.
I hope this is useful to you. If it would help more I could send you a self-extracting zip file of the program.
Below is the code for the project.
Rowland

Option Explicit
Dim naRandIndexArray(1 To 1000)
Dim naRandNumArray(1 To 1000)
Dim naRandSearchArray(1000 To 5000)

Private Sub cmdBuildIndexArray_Click()
Dim nFirstLoop2 As Integer
Dim nSecondLoop2 As Integer
Dim RandIndex As Integer
Dim RandIndexTotal As Long
Dim RandIndexOK As Boolean

Randomize
For nFirstLoop2 = 1 To 1000
RandIndex = Int(5001 * Rnd)
If RandIndex > 999 And RandIndex < 5001 Then
For nSecondLoop2 = 1 To nFirstLoop2
If RandIndex <> naRandIndexArray(nSecondLoop2) Then
RandIndexOK = True
Else
RandIndexOK = False
nFirstLoop2 = nFirstLoop2 - 1
Exit For
End If
Next nSecondLoop2
If RandIndexOK Then
naRandIndexArray(nFirstLoop2) = RandIndex
RandIndexTotal = RandIndexTotal + RandIndex
lblRandIndex.Caption = RandIndexTotal
End If
Else
nFirstLoop2 = nFirstLoop2 - 1
End If
Next nFirstLoop2
cmdBuildSearchArray.Enabled = True
cmdBuildIndexArray.Enabled = False
cmdBuildSearchArray.SetFocus
End Sub

Private Sub cmdBuildNumArray_Click()
Dim nFirstLoop1 As Integer
Dim nSecondLoop1 As Integer
Dim nRandNum As Integer
Dim RandTotal As Long
Dim RandNumOK As Boolean

Randomize
For nFirstLoop1 = 1 To 1000
nRandNum = Int(1001 * Rnd)
If nRandNum > 0 And nRandNum < 1001 Then
For nSecondLoop1 = 1 To nFirstLoop1
If nRandNum <> naRandNumArray(nSecondLoop1) Then
RandNumOK = True
Else
RandNumOK = False
nFirstLoop1 = nFirstLoop1 - 1
Exit For
End If
Next nSecondLoop1
If RandNumOK Then
naRandNumArray(nFirstLoop1) = nRandNum
RandTotal = RandTotal + nRandNum
lblRandNum.Caption = RandTotal
End If
Else
nFirstLoop1 = nFirstLoop1 - 1
End If
Next nFirstLoop1
cmdBuildIndexArray.Enabled = True
cmdBuildNumArray.Enabled = False
cmdBuildIndexArray.SetFocus
End Sub

Private Sub cmdBuildSearchArray_Click()
Dim nStepArrays As Integer
Dim nStepNum As Integer
Dim nStepIndex As Integer
Dim SearchTotal As Long

For nStepArrays = 1 To 1000
nStepNum = naRandNumArray(nStepArrays)
nStepIndex = naRandIndexArray(nStepArrays)
naRandSearchArray(nStepIndex) = nStepNum
SearchTotal = SearchTotal + naRandSearchArray(nStepIndex)
lblBuildSearch.Caption = SearchTotal
Text4.Enabled = True
Text4.SetFocus
cmdBuildSearchArray.Enabled = False
Next nStepArrays

End Sub

Private Sub cmdQuit_Click()
End
End Sub

Private Sub cmdSearch_Click()
Dim lSearch As Integer
Dim nCheckNum As Integer

lblSearch.Caption = &quot;&quot;
For lSearch = 1000 To 5000
nCheckNum = naRandSearchArray(lSearch)
If nCheckNum = Text4.Text Then
lblSearch.Caption = nCheckNum & &quot; - &quot; & lSearch
Exit For
Else
lblSearch.Caption = &quot;No Match&quot;
End If
Next
cmdSearch.Enabled = False
End Sub


Private Sub Form_Load()
cmdBuildIndexArray.Enabled = False
cmdBuildSearchArray.Enabled = False
cmdSearch.Enabled = False
Text4.Enabled = False


End Sub

Private Sub Text4_Change()
cmdSearch.Enabled = True
End Sub
[sig][/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top