View Full Version : Array problem

09-28-2011, 08:09 PM
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:

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(/(.*?) \((.*?) - (.*?)\)/);


//This yields "Tony Romo"

//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].


09-28-2011, 08:42 PM
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...

for (var i in myPlayersArray[0]){
alert(myPlayersArray[0][i]); // This should show no alerts

or you could even interogate the array directly


Old Pedant
09-28-2011, 08:56 PM
Not to ask a silly question, but have you checked to make sure there are no leading or trailing spaces?

Maybe do this:

if(pMatches) {
myPlayersArray.push( pMatches[1].replace(/^\s+/,"").replace(/\s+$/,"") );

to ensure you have trimmed off all spaces.

09-28-2011, 09:36 PM
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!

Old Pedant
09-28-2011, 10:44 PM
See, I looked at your code here:

var pMatches = stripPlayerColumnElement.match(/(.*?) \((.*?) - (.*?)\)/);

And realized that if your text was


(where each ^ represents a space character) then the " \((.*?) - (.*?)\)" part of your match would only be matching the part in red below:


So then pMathes[1] would be


If you changed to

var pMatches = stripPlayerColumnElement.match(/(.*?)\s+\((.*?) - (.*?)\)/);

Then that would match


But it still wouldn't get rid of the leading spaces.

I *think* you could use

var pMatches = stripPlayerColumnElement.match(/^\s*(.*?)\s+\((.*?) - (.*?)\)/);

But I don't see anything wrong with my hack to do the trim afterwards.