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 5 of 5

Thread: Array problem

  1. #1
    New Coder
    Join Date
    Oct 2009
    Posts
    30
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Array problem

    I have been beating my head against the wall trying to figure this out for hours. It seems like it should be so easy, but here is my code:

    Code:
    var tempMyPlayersArray = document.getElementsByClassName("player");	
    	var myPlayersArray = new Array();
    	
    	for (var p=0; p<playerColumnTeamArray.length; p++) {
    		var stripPlayerColumnElement = playerColumnTeamArray[p].innerHTML.stripTags();
    		var pMatches = stripPlayerColumnElement.match(/(.*?) \((.*?) - (.*?)\)/);
    		
    		if(pMatches)
    			myPlayersArray.push(pMatches[1]);
    	}
    
    //This yields "Tony Romo"
    GM_log(myPlayersArray[0]);   
    
    //This yields "false". 
    GM_log((myPlayersArray[0]) == 'Tony Romo');
    Note that stripTags() removes all of the html tags and leaves you with the folowing (or similar) for every element:

    Tony Romo (Dal - QB)

    So my overall question is why that last GM_log statement in the code is yielding false. I have made sure that the elements are all strings (using toString()), checked for white space issues (using repace()), and made sure the regex is working okay. No matter what though, I get false even though "Tony Romo" is clearly the only string in myPlayersArray[0].

    Thanks!

  • #2
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    It does look like that is the only value in your array, but as I can't get your code to run I substituted values in your array, and your GM_log syntax worked fine. The problem is in your array. I have only the following suggestions:
    Try the following to make sure there are no other hidden values in your array...
    Code:
    for (var i in myPlayersArray[0]){
     alert(myPlayersArray[0][i]);  // This should show no alerts
     }
    or you could even interogate the array directly
    Code:
    alert(myPlayersArray.length);

  • Users who have thanked blaze4218 for this post:

    bszen27 (09-28-2011)

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,155
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    Not to ask a silly question, but have you checked to make sure there are no leading or trailing spaces?

    Maybe do this:
    Code:
    		if(pMatches) {
    			myPlayersArray.push( pMatches[1].replace(/^\s+/,"").replace(/\s+$/,"") );
                    }
    to ensure you have trimmed off all spaces.
    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.

  • Users who have thanked Old Pedant for this post:

    bszen27 (09-28-2011)

  • #4
    New Coder
    Join Date
    Oct 2009
    Posts
    30
    Thanks
    9
    Thanked 0 Times in 0 Posts
    I guess it wasn't as silly as you thought, because that seemed to be the issue. What I don't understand is that I actually tried doing the same thing previously using a slightly different code and I still couldn't get it to match. Either way, thanks for your help, because there was no way I could move further into the userscript without getting by that.

    Thanks again!

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,155
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    See, I looked at your code here:
    Code:
    var pMatches = stripPlayerColumnElement.match(/(.*?) \((.*?) - (.*?)\)/);
    And realized that if your text was
    Code:
        ^^^Tony^Romo^^^(Dal^-^QB)
    (where each ^ represents a space character) then the " \((.*?) - (.*?)\)" part of your match would only be matching the part in red below:
    Code:
        ^^^Tony^Romo^^^(Dal^-^QB)
    So then pMathes[1] would be
    Code:
        ^^^Tony^Romo^^
    If you changed to
    Code:
    var pMatches = stripPlayerColumnElement.match(/(.*?)\s+\((.*?) - (.*?)\)/);
    Then that would match
    Code:
        ^^^Tony^Romo^^^(Dal^-^QB)
    But it still wouldn't get rid of the leading spaces.

    I *think* you could use
    Code:
    var pMatches = stripPlayerColumnElement.match(/^\s*(.*?)\s+\((.*?) - (.*?)\)/);
    But I don't see anything wrong with my hack to do the trim afterwards.
    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
    •