Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    finding a substring in an array?

    ok so i have it so that ill type a command in like /ban @name and itll
    take everything after @ and put it into a variable (substring method).

    i also have a users list which takes down everyones user name and user id in the chat room im in and puts it into an array in the format name=userid

    so lets say someone is represented in the userlist array as
    johnson = 1000000000

    if i try to use array.indexOf(substring) it returns -1 every time....

    even if substring = johnson

    so long story short my question is how would i compare my substring to the users list and have it find their name which is attached to their user id and give me the index location of their name, strip the user id out of it and put it into a different array.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,584
    Thanks
    78
    Thanked 4,385 Times in 4,350 Posts
    The problem is that Array.indexOf and String.indexOf are very very different.

    String.indexOf indeed looks for a substring in the string.
    But Array.indexOf looks for an array *ELEMENT* (the ENTIRE element) that matches the given test value.

    So the quick answer to your question is: Write code!

    Code:
    function findSubstringInArray( ary, str )
    {
        for ( var a = 0; a < ary.length; ++a )
        {
            if ( ary[a].indexOf(str) >= 0 ) { return a; } /* return element number of found match */
        }
        return -1; /* not found */
    }
    Now... If this is something you do all the time, you might want to extend the Array prototype to add a method that will do as you wish. But if it's something you only do in one place in one page, just use simple code as shown.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,584
    Thanks
    78
    Thanked 4,385 Times in 4,350 Posts
    I don't understand this part of your post:
    give me the index location of their name, strip the user id out of it and put it into a different array.
    I did the "give me the index location".

    But I don't understand the point of putting the result(s) in a different array.

    Presumably, there will only be one result, so why does it need to go into an array??

    And even if you modified my code to find multiple results, after stripping out the userids all that is left are the numeric values, so the new array would end up looking like
    Code:
    [ 1000000, 1887331, 2000181 ]
    How would that be useful?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,584
    Thanks
    78
    Thanked 4,385 Times in 4,350 Posts
    I also have to ask: Suppose your incoming array looks like this:
    Code:
    [  "adamson = 123", "jones = 775", "adams = 999" ]
    If you ask for "adams", you are going to find "admason" first. Is that really what you want?

    Seems to me like this kind of array is a mistake. I think you would want an object with multiple properties, thus:
    Code:
    var userinfo = {
        "adamson" : 123,
        "jones" : 775,
        "adams" : 999
        };
    And now you could just do
    Code:
        var value = userinfo["adams"];
    Much more efficient and you won't get "false positive" matches.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •