1

I am new here. I am trying to change the following tuple into multi-dimensional array in javascript. tuple string: str: "(4,7,1),(8,9,6),(3,5,7)". I can think of doing it with for loop. I am looking for sufficient way to do this. One way to do is to use RegEx in javascript. I am not good in Regular expression. Could anyone help me please? Thanks in advance!

TaoPR
  • 5,932
  • 3
  • 25
  • 35
alps07
  • 19
  • 2
  • 6

2 Answers2

0

You can just parse it yourself. Here's one way in a working snippet:

function parseTuple(t) {
    var items = t.replace(/^\(|\)$/g, "").split("),(");
    items.forEach(function(val, index, array) {
       array[index] = val.split(",").map(Number);
    });
    return items;
}

var data = "(4,7,1),(8,9,6),(3,5,7)";
var result = parseTuple(data);
document.write(JSON.stringify(result));

You could also use JSON.parse() to do the parsing for you by converting the tuple string into legal JSON like this:

function parseTuple(t) {
    return JSON.parse("[" + t.replace(/\(/g, "[").replace(/\)/g, "]") + "]");
}

var t = "(4,7,1),(8,9,6),(3,5,7)";
var result = parseTuple(t);
document.write(JSON.stringify(result));
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Is it better to use `nums.map(Number)`? – TaoPR Jul 05 '15 at 17:39
  • @TaoP.R. - I've added that improvement. Thx. – jfriend00 Jul 05 '15 at 17:41
  • Thanks for replying! I was missing the regex. My function should be efficient. So I don't know using "map" will increase the execution time.Please help if I am wrong. I trying to learn about making my code efficient with balance of memory used and execution time. – alps07 Jul 05 '15 at 17:43
  • @alps07 - that regex is just to get rid of the leading and trailing parens. – jfriend00 Jul 05 '15 at 17:44
  • @jfriend00 - do you think JSON solution will be faster than map solution?Which one is more efficient? – alps07 Jul 05 '15 at 19:05
  • @alps07 - All performance questions have to be answered by measuring a real-world scenario. Unless your tuples are huge, I can't imagine performance differences would be a big deal here. But `JSON.parse()` may be implemented in native code so it could be faster, but it is more general purpose so it may have more to check. If you really want to know which is faster, then you'd have to run a performance benchmark in a tool like jsperf.com with a representative data set in multiple browsers to measure. – jfriend00 Jul 05 '15 at 19:08
-1

You can use eval. Here, this is your prototype function tuple.

String.prototype.tuple = function(){
    return eval("[" + this.replace(/\(/g,"[").replace(/\)/g,"]") + "]")
}

And this is your example usage of this prototype function.

var s = "(4,7,1),(8,9,6),(3,5,7)";
console.log(s.tuple())

As you see it is easy and fast, you can try it using jsFiddle below. All you need to use s.tuple()

Here is the jsFiddle

Why do I want to use eval and not JSON.parse?

If you are sure about your input, using eval() is lot more better than JSON.parse().

  • Some browsers still does not support JSON.parse()
  • JSON.parse() uses eval() too, check this
  • eval() is lot more faster than JSON.parse()

String.prototype.tuple = function(){
    return eval("[" + this.replace(/\(/g,"[").replace(/\)/g,"]") + "]")
}

var s = "(4,7,1),(8,9,6),(3,5,7)";
document.write(JSON.stringify(s.tuple()))
Ahmet Can Güven
  • 5,392
  • 4
  • 38
  • 59
  • 1
    What's your reasoning behind using `eval` rather than `JSON.parse`? – Trey Cordova Jul 05 '15 at 17:43
  • JSON.parse uses eval() too. If you are sure about your input because of security then using eval() is lot more faster than JSON.parse. – Ahmet Can Güven Jul 05 '15 at 17:46
  • 1
    @AhmetCanGüven - `JSON.parse()` does NOT use `eval()`. It parses the JSON in a much safer way that is not vulnerable to embedded code attacks. That's the main reason it is used instead of `eval()`. – jfriend00 Jul 05 '15 at 18:29