Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to convert a string like this "10|15|1,hi,0,-1,bye,2" where the first two elements 10|15 mean something different than 1,hi,0,-1,bye,2. I would like to separate them from each other. A naive way to accomplish that would be:

value = string.split("|");
var first = value[0];
var second = value[1];
var tobearray = value[2];
array = tobearray.split(",");

(Of course, if you know a way to do this in a better way, I'd be glad to know). However, array is an array which contains array[0]=1, array[1]=hi, array[2]=0, array[3]=-1, etc. However, I want to obtain a two dimensional array such as

array[0][0]=1, array[0][1]=hi, array[0][2]=0
array[1][0]=-1, array[1][1]=bye, array[1][2]=2

Is there any way to do that?

Thanks

share|improve this question
    
Why do you want a multi-d array instead of a sparse array-like object {10: [1,'hi',0], 15: [-1,'bye',2],}? –  kojiro Mar 2 '11 at 5:50
    
@kojiro: Well, I was using google-dif-match-patch which produces array(array())'s, therefore, in order to maintain consistency I chose to use multidimensional arrays. –  Robert Smith Mar 2 '11 at 5:54
    
@Robert ah, I see. Just curious! –  kojiro Mar 2 '11 at 5:59
    
@kojiro: It's ok. Should I be worry about performance? –  Robert Smith Mar 2 '11 at 6:02
    
Not if your arrays are really this small! ;) Seriously, I would expect the object to be less efficient than the array, but depending on use, possibly easier to work with. –  kojiro Mar 2 '11 at 6:04

3 Answers 3

up vote 8 down vote accepted

The first two elements (10|15) can be extracted beforehand. After that you're left with:

var a = "1,hi,0,-1,bye,2";

Let's splice until we're left with nothing:

var result = [];

a = a.split(','); 

while(a[0]) {
    result.push(a.splice(0,3));
}

result; // => [["1","hi","0"],["-1","bye","2"]]
share|improve this answer
    
Works great!.Thanks. –  Robert Smith Mar 2 '11 at 5:47
function getMatrix(input_string) {
    var parts = input_string.split('|');
    var subparts = parts.pop().split(',');
    var partlen = subparts.length / parts.length;
    for (var i=0; i<parts.length; i++) {
        parts[i] = subparts.splice(0,partlen);
    }
    return parts;
}
share|improve this answer
    
Oh, very nice!. Unfortunately, J-P posted a very good solution earlier. +1, though. Thanks :-) –  Robert Smith Mar 2 '11 at 5:51
function getMatrix(input_string) 
{
    var parts = input_string.split('^');
    for (var t=0; t<parts.length; t++)
    {
        var subparts = parts[t].split('*');

        parts[t] = subparts.splice(0,subparts.length);              
    }

    return parts;
}
share|improve this answer

protected by H2CO3 Sep 19 '12 at 12:24

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.