Results 1 to 15 of 15

Thread: Comparing number vs number in an array

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2010
    Posts
    4

    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!

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    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

  3. #3
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: Comparing number vs number in an array

    Just out of interest, why are you using an array strings to store numbers?
    W o t . S i g

  4. #4
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    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.

  5. #5
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: Comparing number vs number in an array

    School?
    W o t . S i g

  6. #6
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    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.

  7. #7

    Thread Starter
    New Member
    Join Date
    Oct 2010
    Posts
    4

    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

  8. #8
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    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...
    Code:
     11 
    101
    W o t . S i g

  9. #9

    Thread Starter
    New Member
    Join Date
    Oct 2010
    Posts
    4

    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.

  10. #10
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    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

  11. #11
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    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

  12. #12
    Frenzied Member
    Join Date
    Jan 2010
    Location
    Connecticut
    Posts
    1,687

    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

  13. #13
    Junior Member
    Join Date
    Oct 2010
    Posts
    31

    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.


    Quote Originally Posted by dilettante View Post
    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.
    Quote Originally Posted by Milk View Post
    School?
    Quote Originally Posted by baja_yu View Post
    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...

  14. #14
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Comparing number vs number in an array

    Quote Originally Posted by krizcillz View Post
    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.

  15. #15
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    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
  •  



Click Here to Expand Forum to Full Width