-
Oct 28th, 2010, 02:38 PM
#1
Thread Starter
New Member
Comparing number vs number in an array
Hi there,
I am looking for a "safe" way to compare a number with an array full of numbers.
Example:
Code:
Sub CheckSomething
Dim levelArray(5) As String
Dim toSearch As String
levelArray(0) = 1
levelArray(1) = 2
levelArray(2) = 3
levelArray(3) = 10
levelArray(4) = 12
toSearch = 31
If InStr(Join(levelArray), toSearch) > 0 Then
MsgBox ("Found " & toSearch)
End If
End Sub
The problem with my example is that we are searching a string, so if toSearch is for examle 31, then it will say "found" because it contains a 1. Same for 21 etc..
What I am looking for is a way to search the exact number in my array and not just showing "found" because the 1 in in the number 31.
Maybe you could be so nice and post a working example to compare exact numbers as this will be very helpful to me. Also, if there is a better was to create an array, feel free to let me know so I can optimize the code. I come from the PHP corner and it'smuch easier to do with php
Thanks!
-
Oct 28th, 2010, 03:45 PM
#2
Re: Comparing number vs number in an array
Loop through array and exit if number was found. Try some like this:
Code:
For i = 0 To Ubound(myArray)
If CLng(myArray(i)) = 123 Then
MsgBox "Exact match was found on line " & i + 1
Exit For
End If
Next i
-
Oct 28th, 2010, 06:27 PM
#3
Re: Comparing number vs number in an array
Just out of interest, why are you using an array strings to store numbers?
-
Oct 28th, 2010, 06:42 PM
#4
Re: Comparing number vs number in an array
I'm curious where all of these people are getting VB6. It seems like a very odd choice for a newb, epecially since it isn't readily available anymore.
-
Oct 28th, 2010, 06:47 PM
#5
Re: Comparing number vs number in an array
-
Oct 28th, 2010, 06:58 PM
#6
Re: Comparing number vs number in an array
A lot of schools here are using it for teaching intro programming for non-strictly IT majors.
-
Oct 29th, 2010, 03:19 AM
#7
Thread Starter
New Member
Re: Comparing number vs number in an array
@Milk: The reason is that I am working on a game server and have created a "level up" function. In this case it's a resource like woodcutting, so I compare the number of how many times you tried to cut a tree to the array, and if the number of tries matches a number in the array, then the woodcutting level goes up by one.
@dilettante I worked with VB6 many years ago but then got very active with PHP so I forgot most of the things. Sure, VB is outdated etc. but I managed to get my hand on a solid multiplayer game engine (written in vb) and I am working on it to release a free multiplayer online game. I remember most of the VB stuff, but it's mostly the simple stuff that I forgot - so I have to ask somewhere
In general: VB might be outdated etc, but I feel that for some reason it works out better than .net in most areas. Sure, it's a pain to have people install all the runtime stuff etc. but in the end all MS languages die and are quickly replaced with something they want you to use.
@RhinoBull thank you for your help
-
Oct 29th, 2010, 05:06 AM
#8
Re: Comparing number vs number in an array
Hi Mabbs, my question was not so much why an array, but why an array of strings.
If you wish to compare an integer to an array of integers then it makes sense to use an integer data type such as Long, Integer or Byte. It will be quicker, use less memory and be less prone to errors.
Code:
Sub CheckSomething
Dim levelArray(5) As Integer
Dim toSearch As Integer
'...
As to possible errors look at the following
Code:
Dim Num1 As Long, Num2 As Long
Num1 = 10
Num2 = 1
Debug.Print Num1 + Num2
Dim sNum1 As String, sNum2 As String
sNum1 = 10
sNum2 = 1
Debug.Print sNum1 + sNum2
The results are...
-
Oct 29th, 2010, 05:32 AM
#9
Thread Starter
New Member
Re: Comparing number vs number in an array
Hi Milk,
I guess the reason why I used a string is because it was the only working example I could find on the net. I will change it to Integer as you have suggested as it makes more sense since it's all about numbers.
On a side-note: With PHP I can easily create an array like:
$lA = array(1, 2, 3, 4, 5);
and also easily search it. I wonder if an array can be created which is similar to the PHP one because it would save some lines.
Right now I use
lA(0) = 1
lA(1) = 2
lA(2) = 5
lA(3) = 10
so I wonder if there is a similar array like the php one like $lA = array(1, 2, 3, 4, 5); so I could save some typing and just add the numbers without haing to to all the lA(0) lA(1) and so on.
Again, thanks for taking your time to help.
-
Oct 29th, 2010, 06:41 AM
#10
Re: Comparing number vs number in an array
The simpler way could be to use assign numbers to a string variable and then split string into array:
Code:
Private Sub Command1_Click()
Dim arNumbers() As Long
Dim strNumbers As String
Dim i As Integer
strNumbers = "1,2,3,4,5,6,7,8,9,10"
arNumbers = Split(strNumbers, ",")
For i = 0 To UBound(arNumbers)
Debug.Print arNumbers(i)
Next i
End Sub
-
Oct 29th, 2010, 06:47 AM
#11
Re: Comparing number vs number in an array
You may also use Array function however the drawback with this method is that array variable has to be variant type:
Code:
Private Sub Command1_Click()
Dim arNumbers() As Variant
Dim i As Integer
arNumbers = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
For i = 0 To UBound(arNumbers)
Debug.Print arNumbers(i)
Next i
End Sub
-
Oct 29th, 2010, 07:21 AM
#12
Re: Comparing number vs number in an array
I know this will not help you, but just so you know, it is east to create an array in VB.NET:
Code:
Dim ArrayNumbers As Integer() = {1, 2, 5, 10, 31}
There are a lot of improvements if you wish to bite the bullet and learn them. Although you already have your engine, but if you wish to code anything new you may wish to consider .NET.
VB6 Library
If I helped you then please help me and rate my post!
If you solved your problem, then please mark the post resolved
-
Oct 31st, 2010, 05:28 AM
#13
Junior Member
Re: Comparing number vs number in an array
@OP:
Are you saying that is a number in your string and you want to find that exact number? The problem occurs when you search for 32 and 432 is in the string, not 32?
What you can do is just check the characters before and after the string found to see if they're numeric. If not then you've found your string.
Hope that helps.
Originally Posted by dilettante
I'm curious where all of these people are getting VB6. It seems like a very odd choice for a newb, epecially since it isn't readily available anymore.
Originally Posted by Milk
School?
Originally Posted by baja_yu
A lot of schools here are using it for teaching intro programming for non-strictly IT majors.
Most schools can't afford to upgrade their software and their teachers don't have the skill sets to teach them the new software (As they have been teaching, not in industry learning new software).
That's the case in England anyway.
When I was in school programming was taught in Excel for those reasons!
Also a lot of students don't know what goes into programming. GUI in vb6 is nice and straight forward. Good Intro. Also has decent feedback and auto compile.
Programming c++ in command line isn't really practical for learners.
Experience with .Net:
Open:"Ooo, this is pretty"
Few minutes later:"Wow lots of useful Controls"
After an hour or so:"Hmm... seem to be doing very little programming here"
Some time later:"WHAT HAVE THEY DONE WITH MY CONTROL ARRAYS?!?!?!"
vb6:Ahh...
-
Oct 31st, 2010, 11:44 AM
#14
Re: Comparing number vs number in an array
Originally Posted by krizcillz
Most schools can't afford to upgrade their software and their teachers don't have the skill sets to teach them the new software (As they have been teaching, not in industry learning new software)...
I don't disagree with that at all however...
Most schools usually get tremendous discounts on software/hardware, besides there are Express editions (c#, vb, etc) available for free so it's kind of hard to believe they can't find budget to upgrade.
Also teachers that don't like improving themselves shouldn't be teaching in the first place.
-
Nov 1st, 2010, 12:45 AM
#15
Re: Comparing number vs number in an array
My old technique for this type of problem is to pad with a symbol -- I usually used dot (.) -- and then search for my search term wrapped in dots. eg:
".1.3.10.12.31."
".31." = found
".2." = not found
There are a number of reasons why I wouldn't use this technique now, but most of them are nitpicky and ego-driven. This down and dirty approach works fine in a pinch.
EDIT: Don't forget to add the symbol to the front and back of the Join. For example:
Code:
MyText = "." & Join(MyArray, ".") & "."
Search = "." & MySearchTerm & "."
Last edited by Ellis Dee; Nov 1st, 2010 at 12:49 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|