Split javascript string into array - javascript
I have this javscript string:
response
"[[[#],[b,#],[b,w,b,b,b,#],[b,#],[b,w,w,b,#]],[[b,#],[b,b,#],[b,b,w,#],[b,b,b,#],[b,b,#]],[[b,#],[b,b,b,b,#],[b,w,#],[b,b,b,#],[b,#]],[[b,#],[b,#],[w,w,w,#],[b,b,w,w,#],[b,w,#]],[[b,#],[b,b,b,b,#],[b,w,b,#],[b,w,#],[b,b,#]]]"
This corresponds to a board game and which field (e.g [b,w,b,b,b,#]) is a cell with black and white pieces. The # is the top of the stack.
I need to parse this in order to create an array of tiles.
I have this:
XMLscene.prototype.readBoard = function(data){
var response = data.target.response;
console.log("REPONSE NO PARS" + response);
response = response.split("],");
console.log("REPONSE " + response);
response[0] = response[0].substring(1);
response[5] = response[5].substring(0, response[5].length - 2);
for(var i = 0; i < response.length; i++)
{
response[i] = response[i].substring(1);
response[i] = response[i].split("),");
for(var j = 0; j < response[i].length; j++)
response[i][j] = response[i][j].substring(5);
}
this.scene.board.loadTiles(response);
//this.scene.client.getPrologRequest('quit', 0, 1);
};
to be parsed in this function:
gameBoard.prototype.loadTiles = function(board){
console.log("BOARD : " + board);
this.tiles = [];
for (var i = 0; i < board.length; i++){
for (var j = 0; j < board[i].length; j++){
var player = board[i][j].split(",")[0];
console.log("PLAYER : " + player);
var type = board[i][j].split(",")[1];
console.log("Type : " + type);
if (type != "e") {
var tile = this.createTile(type, this.scene ,i*6 + j+100, player);
tile.line = i;
tile.col = j;
this.tiles.push(tile);
}
}
}
}
The board structure I want is something like this:
for the first stack: [#]
It's an empty cell
[b,#] - A cell with one piece - black
[b,w,w,b,#] - A cell with a black piece in the bottom, then two white pieces and a black on the top, therefore the black player is the owner of the stack!
The stack owner is the player that have his piece on the top of the stack (closest to #)
Is there any way to get an array with each stack being the element of it?
Regards
You could transform the data to JSON like this, ignoring the hashes as they seem to give information that is already known (the stack ends at the right):
var response = JSON.parse(response.replace(/,?#/g, '').replace(/[bw]/g, '"$&"'));
Then you can for instance identify the current player for a stack at (i, j), like this:
var player = board[i][j].slice(-1)[0]; // last element on the stack
Snippet:
// Sample data
var response = "[[[#],[b,#],[b,w,b,b,b,#],[b,#],[b,w,w,b,#]],[[b,#],[b,b,#],[b,b,w,#],[b,b,b,#],[b,b,#]],[[b,#],[b,b,b,b,#],[b,w,#],[b,b,b,#],[b,#]],[[b,#],[b,#],[w,w,w,#],[b,b,w,w,#],[b,w,#]],[[b,#],[b,b,b,b,#],[b,w,b,#],[b,w,#],[b,b,#]]]";
// Convert to nested array
var board = JSON.parse(response.replace(/,?#/g, '').replace(/[bw]/g, '"$&"'));
// Print the stack at 3, 3
console.log(board[3][3].join(','));
// Print player for that stack:
console.log(board[3][3].slice(-1)[0]);
A quick and dirty solution is to quote all your elements by using String.prototype.replace() and then put the entire result in an eval():
var str = "[[[#],[b,#],[b,w,b,b,b,#],[b,#],[b,w,w,b,#]],[[b,#],[b,b,#],[b,b,w,#],[b,b,b,#],[b,b,#]],[[b,#],[b,b,b,b,#],[b,w,#],[b,b,b,#],[b,#]],[[b,#],[b,#],[w,w,w,#],[b,b,w,w,#],[b,w,#]],[[b,#],[b,b,b,b,#],[b,w,b,#],[b,w,#],[b,b,#]]]";
var res = eval(str.replace(/[bw#]/g, "'$&'"));
console.log(res);
Modify your string to look like this...
var myString = '[[[#],[b,#],[b,w,b,b,b,#],[b,#],[b,w,w,b,#]],[[b,#],[b,b,#],[b,b,w,#],[b,b,b,#],[b,b,#]],[[b,#],[b,b,b,b,#],[b,w,#],[b,b,b,#],[b,#]],[[b,#],[b,#],[w,w,w,#],[b,b,w,w,#],[b,w,#]],[[b,#],[b,b,b,b,#],[b,w,b,#],[b,w,#],[b,b,#]]]'
replace elements with ""
Now run following:
var myArrayObject = JSON.parse(myString);
You just converted it to array.
Sample code:
https://fiddle.jshell.net/3gvzmwef/21/
Related
Joining intersecting paths in illustrator
Short term problem: I have three paths on an artboard. The end of one path has a point at the same position as the beginning of another. The other path is separate. They are all grouped. I have some code that loops through the pathsin the group, and if one path ends where another begins it tried to join them together. The group must be highlighted. To start with my artboard look like this (The top line is two paths): An after the script is run it looks like this: With a lot of points added to the end of the line underneath. Could someone lend me a hand with this, Ideally, I'd like it to look like this: The code looks like this: var doc = activeDocument;//Gets the active document var numArtboards = doc.artboards.length;//returns the number of artboards in the document var intersections = true var group = doc.selection[0] var paths = [] var intersecttions = 0 // Builds an array of all the paths in the grouped object if (group !== undefined && group.pageItems.length >= 2) { for (var i = 0; i < group.pageItems.length; i++) { var item = group.pageItems[i]; if (item instanceof PathItem) { item.id = 'Path No' + i; paths.push(item) } } } //Sets the first path that will be added to $.write('paths length ', paths.length,'\n') var chain = paths[0] var chainPoints = chain.pathPoints var chainLength = chainPoints.length - 1 var c1 = chainPoints[0] var c2 = chainPoints[chainLength] $.write('c ', c1.anchor,':::', c2.anchor,'\n') //loops through the paths in the group to see if any overlap the first past for (var i = 1; i < paths.length-1; i++) { var link = paths[i] $.write(link, '\n') var linkPoints = link.pathPoints var linkLength = linkPoints.length - 1 $.write('l ', l1.anchor, ':::', l2.anchor, '\n') if (toString(c1.anchor) === toString(l2.anchor)) { $.write('inttersection', '\n') $.write('link', link.id, '\n') for (var j = 0; j < linkLength; ++j) { chain.pathPoints.add(linkPoints[j]) $.write (linkPoints[j], '\n') } } }
The first problem is that it's not detecting the instance of overlap correctly. The line: if (toString(c1.anchor) === toString(l2.anchor)) { is not comparing one string to another but comparing a true response with another true response. It should be: if (String(c1.anchor) === String(l2.anchor)) { you also have to pass across the attributes of each point you are adding to the line and remove the old line, so within the j loop you'll need to add the following for (var j = 0; j < linkLength; ++j) { var pp1 = chainPoints.add() var p2i = linkPoints[j]; pp1.anchor = p2i.anchor; pp1.rightDirection = p2i.rightDirection; pp1.leftDirection = p2i.leftDirection; pp1.pointType = p2i.pointType; pp1.handle = p2i.handle; } link.remove(); This seems to work except that it doesn't add the last point of the second line. I'm guessing that the loop length may not be set correctly If I work it out I'll update the post. I found this in Hiroyuki Sato code for his JoinReasonable scripts http://shspage.com/aijs/en/
Pull information from a data file?
I have over 170000 entries of data in data file in this format: 1479661:-1,1,-1,-898,-769,0.00;-1,2,-1,-96,-1402,0.00;-1,3,-1,117,-1397,0.00;-1,4,-1,-4,-2420,0.00;4,5,-1,5570,4395,0.00;4,6,-1,5570,4395,0.00;4,7,-1,5570,4395,0.00;4,8,-1,5570,4395,0.00;4,9,-1,5570,4395,0.00;4,10,-1,5570,4395,0.00;4,11,-1,5570,4395,0.00;4,12,-1,5570,4395,0.00;4,13,-1,5570,4395,0.00;4,14,-1,5570,4395,0.00;-1,15,-1,913,-3533,0.00;4,16,-1,5570,4395,0.00;4,17,-1,5570,4395,0.00;4,18,-1,5570,4395,0.00;4,19,-1,5570,4395,0.00;4,20,-1,5570,4395,0.00;4,21,-1,5570,4395,0.00;4,22,-1,5570,4395,0.00;4,23,-1,5570,4395,0.00;4,24,-1,5570,4395,0.00;4,25,-1,5570,4395,0.00;4,26,-1,5570,4395,0.00;4,27,-1,5570,4395,0.00;4,28,-1,5570,4395,0.00;4,29,-1,5570,4395,0.00;:117,-1397,7,7.00,A,Dead;: 1479662:-1,1,-1,-898,-769,0.00;-1,2,-1,-96,-1402,0.00;-1,3,-1,117,-1392,0.00;-1,4,-1,-6,-2419,0.00;4,5,-1,5570,4395,0.00;4,6,-1,5570,4395,0.00;4,7,-1,5570,4395,0.00;4,8,-1,5570,4395,0.00;4,9,-1,5570,4395,0.00;4,10,-1,5570,4395,0.00;4,11,-1,5570,4395,0.00;4,12,-1,5570,4395,0.00;4,13,-1,5570,4395,0.00;4,14,-1,5570,4395,0.00;-1,15,-1,913,-3533,0.00;4,16,-1,5570,4395,0.00;4,17,-1,5570,4395,0.00;4,18,-1,5570,4395,0.00;4,19,-1,5570,4395,0.00;4,20,-1,5570,4395,0.00;4,21,-1,5570,4395,0.00;4,22,-1,5570,4395,0.00;4,23,-1,5570,4395,0.00;4,24,-1,5570,4395,0.00;4,25,-1,5570,4395,0.00;4,26,-1,5570,4395,0.00;4,27,-1,5570,4395,0.00;4,28,-1,5570,4395,0.00;4,29,-1,5570,4395,0.00;:117,-1392,7,7.07,A,Dead;: Now each data array starts with a unique id and i was wondering is there a convenient way to push each entry 1479661 then 1479662 every second to an array and assign the values within the unique id into 6 fields that update. Now I ask if there is a more convenient way as currently I am using this method: Data comprises of three chunks in a single line Split the link into chunks var chunkOne = [1479661]; var chunkTwo = [-1,1,-1,-898,-769,0.00,-1,2,-1,-96,-1402,0.00,-1,3,-1,117,-1397,0.00,-1,4,-1,-4,-2420,0.00,4,5,-1,5570,4395,0.00,4,6,-1,5570,4395,0.00,4,7,-1,5570,4395,0.00,4,8,-1,5570,4395,0.00,4,9,-1,5570,4395,0.00,4,10,-1,5570,4395,0.00,4,11,-1,5570,4395,0.00,4,12,-1,5570,4395,0.00,4,13,-1,5570,4395,0.00,4,14,-1,5570,4395,0.00,-1,15,-1,913,-3533,0.00,4,16,-1,5570,4395,0.00,4,17,-1,5570,4395,0.00,4,18,-1,5570,4395,0.00,4,19,-1,5570,4395,0.00,4,20,-1,5570,4395,0.00,4,21,-1,5570,4395,0.00,4,22,-1,5570,4395,0.00,4,23,-1,5570,4395,0.00,4,24,-1,5570,4395,0.00,4,25,-1,5570,4395,0.00,4,26,-1,5570,4395,0.00,4,27,-1,5570,4395,0.00,4,28,-1,5570,4395,0.00,4,29,-1,5570,4395,0.00]; var chunkThree = [117,-1397,7,7.00,"A","Dead"]; Then get length of each array: var chunkOneLength = chunkOne.length; var chunkTwoLength = chunkTwo.length; var chunkThreeLength = chunkThree.length; Pick out the nth value in the array depending on the data chunk: //uniqueID set as first for (var i = 0; i < chunkOneLength; i = i + 1) { // useful code would go here alert("This is the unique ID " + chunkOne[i]); } //teamval for (var i = 0; i < chunkTwoLength; i = i + 6) { // useful code would go here alert("This is the teamVal " + chunkTwo[i]); } Now the only problem I see with this method, is that the original data array will need to be formatted and separated into chunks every time.
As you have a separator on each section i.e. : you can actually use split to split them up like below and push them into a array and only have to do 2 loops - firstly pushing the split data in a new array and then looping through them and populating the structure. Please note as long as each chunk of data is separated with : this will work with anything even if you expand the data later. obviously it will not work if you remove the : separator. example below: var splitChucks = []; var chucks = [ "1479661:-1,1,-1,-898,-769,0.00;-1,2,-1,-96,-1402,0.00;-1,3,-1,117,-1397,0.00;-1,4,-1,-4,-2420,0.00;4,5,-1,5570,4395,0.00;4,6,-1,5570,4395,0.00;4,7,-1,5570,4395,0.00;4,8,-1,5570,4395,0.00;4,9,-1,5570,4395,0.00;4,10,-1,5570,4395,0.00;4,11,-1,5570,4395,0.00;4,12,-1,5570,4395,0.00;4,13,-1,5570,4395,0.00;4,14,-1,5570,4395,0.00;-1,15,-1,913,-3533,0.00;4,16,-1,5570,4395,0.00;4,17,-1,5570,4395,0.00;4,18,-1,5570,4395,0.00;4,19,-1,5570,4395,0.00;4,20,-1,5570,4395,0.00;4,21,-1,5570,4395,0.00;4,22,-1,5570,4395,0.00;4,23,-1,5570,4395,0.00;4,24,-1,5570,4395,0.00;4,25,-1,5570,4395,0.00;4,26,-1,5570,4395,0.00;4,27,-1,5570,4395,0.00;4,28,-1,5570,4395,0.00;4,29,-1,5570,4395,0.00;:117,-1397,7,7.00,A,Dead;:", "1479662:-1,1,-1,-898,-769,0.00;-1,2,-1,-96,-1402,0.00;-1,3,-1,117,-1392,0.00;-1,4,-1,-6,-2419,0.00;4,5,-1,5570,4395,0.00;4,6,-1,5570,4395,0.00;4,7,-1,5570,4395,0.00;4,8,-1,5570,4395,0.00;4,9,-1,5570,4395,0.00;4,10,-1,5570,4395,0.00;4,11,-1,5570,4395,0.00;4,12,-1,5570,4395,0.00;4,13,-1,5570,4395,0.00;4,14,-1,5570,4395,0.00;-1,15,-1,913,-3533,0.00;4,16,-1,5570,4395,0.00;4,17,-1,5570,4395,0.00;4,18,-1,5570,4395,0.00;4,19,-1,5570,4395,0.00;4,20,-1,5570,4395,0.00;4,21,-1,5570,4395,0.00;4,22,-1,5570,4395,0.00;4,23,-1,5570,4395,0.00;4,24,-1,5570,4395,0.00;4,25,-1,5570,4395,0.00;4,26,-1,5570,4395,0.00;4,27,-1,5570,4395,0.00;4,28,-1,5570,4395,0.00;4,29,-1,5570,4395,0.00;:117,-1392,7,7.07,A,Dead;:"]; for (var i = 0; i < chucks.length; i++){ splitChucks.push(chucks[i].split(':')) } for (var h = 0; h < splitChucks.length; h++) { alert("This is the unique ID " + splitChucks[h][0]); alert("This is the teamVal " + splitChucks[h][1]); } Hope this helps, this is a much more efficient way to do your task :)
var splitChucks = []; var chucks = [ "1479661:-1,1,-1,-898,-769,0.00;-1,2,-1,-96,-1402,0.00;-1,3,-1,117,-1397,0.00;-1,4,-1,-4,-2420,0.00;4,5,-1,5570,4395,0.00;4,6,-1,5570,4395,0.00;4,7,-1,5570,4395,0.00;4,8,-1,5570,4395,0.00;4,9,-1,5570,4395,0.00;4,10,-1,5570,4395,0.00;4,11,-1,5570,4395,0.00;4,12,-1,5570,4395,0.00;4,13,-1,5570,4395,0.00;4,14,-1,5570,4395,0.00;-1,15,-1,913,-3533,0.00;4,16,-1,5570,4395,0.00;4,17,-1,5570,4395,0.00;4,18,-1,5570,4395,0.00;4,19,-1,5570,4395,0.00;4,20,-1,5570,4395,0.00;4,21,-1,5570,4395,0.00;4,22,-1,5570,4395,0.00;4,23,-1,5570,4395,0.00;4,24,-1,5570,4395,0.00;4,25,-1,5570,4395,0.00;4,26,-1,5570,4395,0.00;4,27,-1,5570,4395,0.00;4,28,-1,5570,4395,0.00;4,29,-1,5570,4395,0.00;:117,-1397,7,7.00,A,Dead;:", "1479662:-1,1,-1,-898,-769,0.00;-1,2,-1,-96,-1402,0.00;-1,3,-1,117,-1392,0.00;-1,4,-1,-6,-2419,0.00;4,5,-1,5570,4395,0.00;4,6,-1,5570,4395,0.00;4,7,-1,5570,4395,0.00;4,8,-1,5570,4395,0.00;4,9,-1,5570,4395,0.00;4,10,-1,5570,4395,0.00;4,11,-1,5570,4395,0.00;4,12,-1,5570,4395,0.00;4,13,-1,5570,4395,0.00;4,14,-1,5570,4395,0.00;-1,15,-1,913,-3533,0.00;4,16,-1,5570,4395,0.00;4,17,-1,5570,4395,0.00;4,18,-1,5570,4395,0.00;4,19,-1,5570,4395,0.00;4,20,-1,5570,4395,0.00;4,21,-1,5570,4395,0.00;4,22,-1,5570,4395,0.00;4,23,-1,5570,4395,0.00;4,24,-1,5570,4395,0.00;4,25,-1,5570,4395,0.00;4,26,-1,5570,4395,0.00;4,27,-1,5570,4395,0.00;4,28,-1,5570,4395,0.00;4,29,-1,5570,4395,0.00;:117,-1392,7,7.07,A,Dead;:"]; for (var i = 0; i < chucks.length; i++){ splitChucks.push(chucks[i].split(':')) } for (var h = 0; h < splitChucks.length; h++) { alert("This is the unique ID " + splitChucks[h][0]); alert("This is the teamVal " + splitChucks[h][1]); }
One of the best purposes of an unique ID is to act as an index. So, instead of iterating over your index array (chunkOne), use ID as an Object key! // 1479661:-1,1,-1,-898,-769,0.00;-1,2,-1,-96,-1402,0.00;-1,3,-1,117,-1397,0.00;-1,4,-1,-4,-2420,0.00;4,5,-1,5570,4395,0.00;4,6,-1,5570,4395,0.00;4,7,-1,5570,4395,0.00;4,8,-1,5570,4395,0.00;4,9,-1,5570,4395,0.00;4,10,-1,5570,4395,0.00;4,11,-1,5570,4395,0.00;4,12,-1,5570,4395,0.00;4,13,-1,5570,4395,0.00;4,14,-1,5570,4395,0.00;-1,15,-1,913,-3533,0.00;4,16,-1,5570,4395,0.00;4,17,-1,5570,4395,0.00;4,18,-1,5570,4395,0.00;4,19,-1,5570,4395,0.00;4,20,-1,5570,4395,0.00;4,21,-1,5570,4395,0.00;4,22,-1,5570,4395,0.00;4,23,-1,5570,4395,0.00;4,24,-1,5570,4395,0.00;4,25,-1,5570,4395,0.00;4,26,-1,5570,4395,0.00;4,27,-1,5570,4395,0.00;4,28,-1,5570,4395,0.00;4,29,-1,5570,4395,0.00;:117,-1397,7,7.00,A,Dead;: var data = { 1479661: [ -1,1,-1,-898,-769,0.00,-1,2,-1,-96,-1402,0.00,-1,3,-1,117,-1397,0.00,-1,4,-1,-4,-2420,0.00,4,5,-1,5570,4395,0.00,4,6,-1,5570,4395,0.00,4,7,-1,5570,4395,0.00,4,8,-1,5570,4395,0.00,4,9,-1,5570,4395,0.00,4,10,-1,5570,4395,0.00,4,11,-1,5570,4395,0.00,4,12,-1,5570,4395,0.00,4,13,-1,5570,4395,0.00,4,14,-1,5570,4395,0.00,-1,15,-1,913,-3533,0.00,4,16,-1,5570,4395,0.00,4,17,-1,5570,4395,0.00,4,18,-1,5570,4395,0.00,4,19,-1,5570,4395,0.00,4,20,-1,5570,4395,0.00,4,21,-1,5570,4395,0.00,4,22,-1,5570,4395,0.00,4,23,-1,5570,4395,0.00,4,24,-1,5570,4395,0.00,4,25,-1,5570,4395,0.00,4,26,-1,5570,4395,0.00,4,27,-1,5570,4395,0.00,4,28,-1,5570,4395,0.00,4,29,-1,5570,4395,0.00,117,-1397,7,7.00,"A","Dead" ] // More data... }; Object.keys(data).forEach(function(key) { console.log('This is ID ' + key); data[key].forEach(function(value,index,array) { console.log('Index ' + index + ' of data key ' + key + ':'); console.log(data[key][index]); }); }); Test this on any modern browser's console or node.js instance to see results.
Working on script in google-apps-script, any suggestions?
Problem: I am creating an array, or multiple arrays, to write values, and continue doing so until a certain value is drawn. The long view for this code is to, using these values to match one letter to another. (i.e. The Bombe used in WWII.) Except this will be completely digital. The Bombe was a decrypter that find that one letter is able to get mapped to another. (Enigma example will be posted in comments.) I am trying to decrpyt any message using this. Understanding: I don't remember a lot of code especially since I have never used google-apps-script before. I no basic ideas, but some of the syntax's are different I am getting lost. (i.e.: instead of System.out.print(); it would be Logger.log(data);. That's really all I have gotten from it so far. I have some code worked out which can be found here. Background: This is quite a lot of text to read I understand, but hear me out before you just flag me and move on. The hyperlink posted in the above comment under the words "Enigma example" shows the mapping that I am trying to do in reverse. I however need to create loops and variables that I do not remember how to create. I have information on the Enigma and Bombe: Enigma & Bombe info. I have done some searches on Google and the like, however nothing that I understand or benefit my end goal. All I have gotten is another link, which I will post in the comments, that shows me a basic loop as they put it. What I need help with: I am asking for help in the following: Looping, variables and arrays. Suggestions will be the most valuable to me, because I am here to learn, not get my stuff done done by asking. Ideas: Some ideas I have are, Garbage collectors, multi-dimensional array and/or just a sequence of possibilities. (See the "Enigma & Bombe info" link above. For easy Copy/Pasting: function bombeCode1() { var fastRotor = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N", "O","P","Q","R","S","T","U","V","W","X","Y","Z"]; var mediumRotor = fastRotor; var slowRotor = fastRotor; var rows = 26; var columns = 26; for (var i=0; i < rows ; i++) { Logger.log('Outer Loop: value of i : ' + i); // Logger.log("Partition for Outer Loop"); // Logger.log(" "); var fastRotorValue = fastRotor[i]; for (var j=0; j < columns ; j++) { Logger.log('-Inner Loop value of j : ' +j); //var fastRotorValue = fastRotor[i]; var medRotorValue = mediumRotor[j]; // Logger.log("---- " + fastRotorValue + " " + medRotorValue); for (var k=0; k < 26 ; k++) { // Logger.log('---- XXXX Third Loop value of k : ' + k); //var fastRotorValue = fastRotor[i]; //var medRotorValue = mediumRotor[j]; var slowRotorValue = slowRotor[k]; Logger.log("---- XXXX " + fastRotorValue + " " + medRotorValue + " " + slowRotorValue); }; //var objectNumberValuePair = {"0":"A", "1":"B", "2":"C","3":"D","4":"E","5":"F","6":"G","7":"H","8":"I", // "9":"J","10":"K","11":"L","12":"M","13":"N","14":"O","15":"P","16":"Q","17":"R", // "18":"S","19":"T","20":"U","21":"V","22":"W","23":"X","24":"Y","25":"Z"} // Logger.log(slowRotorValue = objectNumberValuePair); // Logger.log(medRoterValue = objectNumberValuePair); // Logger.log(fastRoterValue = objectNumberValuePair); } } }
Here is the code I modified a little bit, just to show the Loop values in the Logger print out. function bombeCode1() { var fastRotor = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N", "O","P","Q","R","S","T","U","V","W","X","Y","Z"]; var mediumRotor = fastRotor; var slowRotor = fastRotor; var rows = 3; var columns = 6; for (var i=0; i < rows ; i++) { Logger.log('Outer Loop: value of i : ' + i); Logger.log("Partition for Outer Loop"); Logger.log(" "); for (var j=0; j < columns ; j++) { Logger.log('----Inner Loop value of j : ' + j); var fastRoterValue = fastRotor[i]; var medRoterValue = mediumRotor[j]; Logger.log("---- " + fastRoterValue + " " + medRoterValue); for (var k=0; k < 6 ; k++) { Logger.log('---- XXXX Third Loop value of k : ' + k); var fastRoterValue = fastRotor[i]; var medRoterValue = mediumRotor[j]; var slowRotorValue = slowRotor[k]; Logger.log("---- XXXX " + fastRoterValue + " " + medRoterValue + " " + slowRotorValue); }; } } } Run it and see what you think. I'm sure it's not what you need, but got to start somewhere.
Writing an array in Google Spreadsheets
I'm trying to learn javascript, so I decided to code a script in Google Apss Script to list all emails with attachment. Until now, I have this code: function listaAnexos() { // var doc = DocumentApp.create('Relatório do Gmail V2'); var plan = SpreadsheetApp.create('Relatorio Gmail'); var conversas = GmailApp.search('has:attachment', 0, 10) var tamfinal = 0; if (conversas.length > 0) { var tam = 0 var emails = GmailApp.getMessagesForThreads(conversas); var cont = 0; for (var i = 0 ; i < emails.length; i++) { for (var j = 0; j < emails[i].length; j++) { var anexos = emails[i][j].getAttachments(); for (var k = 0; k < anexos.length; k++) { var tam = tam + anexos[k].getSize(); } } var msginicial = conversas[i].getMessages()[0]; if (tam > 0) { val = [i, msginicial.getSubject(), tam]; planRange = plan.getRange('A1:C1'); planRange.setValue(val); // doc.getBody().appendParagraph('A conversa "' + msginicial.getSubject() + '" possui ' + tam + 'bytes em anexos.'); } var tamfinal = tamfinal + tam; var tam = 0; } } } listaAnexos(); It works, but with 2 problems: 1) It writes the three val values at A1, B1 and C1. But I want to write i in A1, msginicial.getSubject() in B1 and tam in C1. 2) How can I change the range interactively? Write the first email in A1:C1, the second in A2:C2 ... I know that are 2 very basic questions, but didn't found on google :(
Problem 1: Make sure you use the right method for the range. You've used Range.setValue() which accepts a value as input, and modifies the content of the range using that one value. You should have used Range.setValues(), which expects an array and modifies a range of the same dimensions as the array. (The array must be a two-dimensional array, even if you're only touching one row.) val = [[i, msginicial.getSubject(), tam]]; planRange = plan.getRange('A1:C1'); planRange.setValues(val); Problem 2: (I assume you mean 'programmatically' or 'automatically', not 'interactively'.) You can either use row and column numbers in a loop say, with getRange(row, column, numRows, numColumns), or build the range string using javascript string methods.
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.
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
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
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.
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')