user3117628
user3117628

Reputation: 786

string occurrences in a string

I'm am working on a script to count the number of times a certain string (in this case, coordinates) occur in a string. I currently have the following:

if (game_data.mode == "incomings") {
var table = document.getElementById("incomings_table");
var rows = table.getElementsByTagName("tr");
var headers = rows[0].getElementsByTagName("th");
var allcoord = new Array(rows.length);
for (i = 1; i < rows.length - 1; i++) {
    cells = rows[i].getElementsByTagName("td");
    var contents = (cells[1].textContent);
    contents = contents.split(/\(/);
    contents = contents[contents.length - 1].split(/\)/)[0];
    allcoord[i - 1] = contents
}}

So now I have my variable allcoords. If I alert this, it looks like this (depending on the number of coordinates there are on the page):

584|521,590|519,594|513,594|513,590|517,594|513,592|517,590|517,594|513,590|519,,

My goal is that, for each coordinate, it saves how many times that coordinate occurs on the page. I can't seem to figure out how to do so though, so any help would be much appreciated.

Upvotes: 0

Views: 94

Answers (4)

Jani Hyyti&#228;inen
Jani Hyyti&#228;inen

Reputation: 5407

I would not handle this as strings. Like, the table, is an array of arrays and those strings you're looking for, are in fact coordinates. Soooo... I made a fiddle, but let's look at the code first.

// Let's have a type for the coordinates
function Coords(x, y) {
    this.x = parseInt(x);
    this.y = parseInt(y);
    return this;
}

// So that we can extend the type as we need
Coords.prototype.CountMatches = function(arr){
    // Counts how many times the given Coordinates occur in the given array
    var count = 0;
    for(var i = 0; i < arr.length; i++){
        if (this.x === arr[i].x && this.y === arr[i].y) count++;
    }
    return count;
};

// Also, since we decided to handle coordinates
// let's have a method to convert a string to Coords.
String.prototype.ToCoords = function () {
    var matches = this.match(/[(]{1}(\d+)[|]{1}(\d+)[)]{1}/);

    var nums = [];
    for (var i = 1; i < matches.length; i++) {
        nums.push(matches[i]);
    }
    return new Coords(nums[0], nums[1]);
};

// Now that we have our types set, let's have an array to store all the coords
var allCoords = [];

// And some fake data for the 'table'
var rows = [
    { td: '04.shovel (633|455) C46' },
    { td: 'Fruits kata misdragingen (590|519)' },
    { td: 'monster magnet (665|506) C56' },
    { td: 'slayer (660|496) C46' },
    { td: 'Fruits kata misdragingen (590|517)' }
];

// Just like you did, we loop through the 'table'
for (var i = 0; i < rows.length; i++) {
    var td = rows[i].td; //<-this would be your td text content
    // Once we get the string from first td, we use String.prototype.ToCoords
    // to convert it to type Coords
    allCoords.push(td.ToCoords());
}

// Now we have all the data set up, so let's have one test coordinate
var testCoords = new Coords(660, 496);

// And we use the Coords.prototype.CountMatches on the allCoords array to get the count
var count = testCoords.CountMatches(allCoords);

// count = 1, since slayer is in there

Upvotes: 0

tewathia
tewathia

Reputation: 7298

You can use the split method.

string.split('517,594').length-1 would return 2

(where string is '584|521,590|519,594|513,594|513,590|517,594|513,592|517,590|517,594|513,590|519')

Upvotes: 0

vagon
vagon

Reputation: 77

Use the .indexOf() method and count every time it does not return -1, and on each increment pass the previous index value +1 as the new start parameter.

Upvotes: 0

Raunak Kathuria
Raunak Kathuria

Reputation: 3225

you can use regular expression like this

"124682895579215".match(/2/g).length;

It will give you the count of expression

So you can pick say first co-ordinate 584 while iterating then you can use the regular expression to check the count

and just additional information

You can use indexOf to check if string present

Upvotes: 1

Related Questions