Prev: FAQ Topic - How do I change the confirm box to say yes/no or default to cancel? (2010-04-26)
Next: Why is this throwing an error?
From: VK on 25 Apr 2010 20:17 While trying to port the promised Shannon's Clairvoyant code to Javascript, I am having real troubles to figure out what that would be in Javascript: 5-dimensional array of 72 elements In VBA it is as simple as: Dim Moves(0 To 1, 0 To 2, 0 To 1, 0 To 2, 0 To 1) As Integer but I am really lost in trying to visualize it so to create an adequate structure . So far I am using the fact of simple object augmentation in Javascript so doing like var Moves = new Object; // ... if (typeof Moves['01001'] == 'undefined') { Moves['01001'] = 1; } else { ++Moves['01001']; } Sorry if the question is not clear enough, I can explain deeper.
From: Thomas 'PointedEars' Lahn on 25 Apr 2010 21:16 VK wrote: > [...] I am having real troubles to figure out what that would be > in Javascript: 5-dimensional array of 72 elements > In VBA it is as simple as: > > Dim Moves(0 To 1, 0 To 2, 0 To 1, 0 To 2, 0 To 1) As Integer > > but I am really lost in trying to visualize it so to create an > adequate structure . Start with the representation of a two-dimensional array, then improve on that. See the FAQ. PointedEars -- Prototype.js was written by people who don't know javascript for people who don't know javascript. People who don't know javascript are not the best source of advice on designing systems that use javascript. -- Richard Cornford, cljs, <f806at$ail$1$8300dec7(a)news.demon.co.uk>
From: Stefan Weiss on 25 Apr 2010 22:09 On 26/04/10 02:17, VK wrote: > While trying to port the promised Shannon's Clairvoyant code to > Javascript, I am having real troubles to figure out what that would be > in Javascript: 5-dimensional array of 72 elements > In VBA it is as simple as: > > Dim Moves(0 To 1, 0 To 2, 0 To 1, 0 To 2, 0 To 1) As Integer There are no multidimensional arrays in JS, only nested arrays. Unfortunately, there's no simple way to create a structure like this with nested arrays, but see below. > if (typeof Moves['01001'] == 'undefined') { > Moves['01001'] = 1; > } .... I don't see anything intrinsically wrong with that. If strings work for you, why not use them. One problem I see with this is when one of the dimensions gets larger than 9. If you want to go with the nested array approach, you'd need loops to create/initialize the structure, or a generator function. Here's a simplistic attempt for a dim "replacement", which I'm sure can be improved on. For example, the DEFAULT_VALUE could be passed as a third argument for each [from, to] group. var DEFAULT_VALUE = 0; function dim () { if (arguments.length) { var args = Array.prototype.splice.call(arguments, 0), fromTo = args.shift(), start = fromTo[0], end = fromTo[1], result = []; while (start <= end) { result[start] = dim.apply(null, args); ++start; } return result; } return DEFAULT_VALUE; } // Dim Moves(0 To 1, 0 To 2, 0 To 1, 0 To 2, 0 To 1) var moves = dim([0, 1], [0, 2], [0, 1], [0, 2], [0, 1]); // ++Moves['01001'] ++moves[0][1][0][0][1]; console.log(moves); > Sorry if the question is not clear enough, I can explain deeper. I'm just curious what Shannon's clairvoyant is going to be like. I know Claude Shannon, and I know a (self-described) clairvoyant, but I don't know that algorithm. I don't doubt that the thing exists, and that this is what it's called in Russian, but I tried googling with various other synonyms for "clairvoyant", no success. I also wasn't able to compile the C code you linked to earlier - it requires some old MS-only include file for console output, and I was too tired at the time to try to understand the source. I swear, one of these days I'm going to learn Russian. There's a huge part of the web that I don't have access to, and that bugs me. When I look at how many people in this group alone are native Russian speakers, I could probably even deduct the cost of a language course from my taxes as a necessary business expense ;) -- stefan
From: nick on 25 Apr 2010 23:25 On Apr 25, 8:17 pm, VK <schools_r...(a)yahoo.com> wrote: > While trying to port the promised Shannon's Clairvoyant code to > Javascript, I am having real troubles to figure out what that would be > in Javascript: 5-dimensional array of 72 elements > In VBA it is as simple as: > Dim Moves(0 To 1, 0 To 2, 0 To 1, 0 To 2, 0 To 1) As Integer Here's one way to emulate multidimensional arrays in languages that lack them. It requires that each dimension but the last have a fixed size, but your case seems to support that. For an example we can use a 2d 5x4 data set. . . . . . . . . . . . . . x . . . . . . Now if this was a 2d array with the first dimension being the column and the second being the row, the 'x' would be at index [3, 2]. var myPos = board[3, 2]; // not gonna work To emulate it with a 1d array we could do this: var myPos = board[3 + 2*5]; In other words, separate your indices with '+' and multiply each index after the first by all previous dimension boundaries. If it was a 3d array, with the dataset shown being element 4 of the first dimension of the array, and the size of the first dimension is 7, it would become: var myPos = board[4 + 3*7 + 2*7*5]; You could turn this idea into a class constructor (not tested): function FixedMultiArray () { this._sizes = arguments; this._data = []; } FixedMultiArray.prototype = new function() { // get an element this.get = function (indexArray) { return this._data[getRealIndex(indexArray)]; } // set an element this.set = function (indexArray, value) { this._data[getRealIndex(indexArray)] = value; } // get the real index in the 1d array function getRealIndex(indexArray) { for (var i=0, realIndex=0; i<indexArray.length; i++) { var ix = indexArray[i]; for (var j=0; j<i; j++) ix *= this._sizes[j]; realIndex += ix; } return realIndex; } } ... var myData = new FixedMultiArray(2,3,2,3,2); myData.set([0,1,0,1,0], 'someValue'); ... I hope this is useful to you. -- Nick
From: VK on 26 Apr 2010 06:57
On Apr 26, 6:09 am, Stefan Weiss <krewech...(a)gmail.com> wrote: <code> On Apr 26, 7:25 am, nick <nick...(a)fastmail.fm> wrote: <code> Great thanks to all of you! I have enough food for thoughts now. P.S. Stefan Weiss: "I swear, one of these days I'm going to learn Russian." It doesn't mean that I'm Russian but I have good courses to recommend. Lucky to study code samples across the Web a particular human language knowledge is not a must. Javascript code is Javascript code on Chinese pages as well :-) P.P.S. Special thanks to Thomas for an educational yet rather hard to use answer. :-) -- "ECMAScript was always an unwanted trade name that sounds like a skin disease." Brendan Eich |