Splitting into a multi-dimensional array

P

PWGSC/TPSGC

I am trying to split a sting into a multi-dimensional array. ex.

value = "lang~1#name~bob#age~37#";

And I want to be able sperate the date to look like

lang = 1
name = bob
age = 37

I was think about having an array created that would be:

value[0][0] = lang
value[0][1] = 1

value[1][0] = name
value[1][1] = bob


value[2][0] = age
value[2][1] = 37

But an open to ideas, drawing a little bit of a mind blank...
 
T

Thomas 'PointedEars' Lahn

PWGSC/TPSGC wrote:
^^^^^^^^^^^
A quite unusual name.
I am trying to split a sting into a multi-dimensional array. ex.

value = "lang~1#name~bob#age~37#";
[...]
I was think about having an array created that would be:

value[0][0] = lang
value[0][1] = 1

value[1][0] = name
value[1][1] = bob

value[2][0] = age
value[2][1] = 37

But an open to ideas, drawing a little bit of a mind blank...

Quickhack:

value = value.split("#");
for (var i = 0; i < value.length; i++)
{
var tmp = value.split("~");
value = new Array();
for (var j = 0; j < tmp.length; j++)
value[j] = tmp[j];
}

But a simple object seems to be better than an Array object:

value = value.split("#");
for (var i = 0; i < value.length; i++)
{
var tmp = value.split("~");
value[tmp[0]] = tmp[1];
}


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top