CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   JavaScript programming (http://www.codingforums.com/forumdisplay.php?f=2)
-   -   Split a string into an 2 d array of substrings: (http://www.codingforums.com/showthread.php?t=286561)

korssane 01-28-2013 04:05 AM

Split a string into an 2 d array of substrings:
 
Hi Guys,

I want to split a string into a 2 d array of substrings like the following :

string = " id1,name1,id2,name2,id3,name3...."
array
element [0][0] = id1
element [0][1] = name1
element [1][0] = id2
element [1][1] = name2
...etc

i tried to tweak the JavaScript split() Method but no result.


Any help ?

thanks

Old Pedant 01-28-2013 04:38 AM

Well, the obvious:
Code:

var s = "id1,name1,id2,name2,id3,name3...."
var temp = s.split(",");
var element = [ ];
for ( var t = 0, e = 0; t < temp.length; t += 2, ++e )
{
    element[e] = [ temp[t], temp[t+1] ];
}

But we could probably get sneakier....

Code:

var s = "id1,name1,id2,name2,id3,name3...."
var temp = s.replace( /([^\,]+\,[^\,]+)\,/g, "$1;" ).split(";");
var element = [];
for ( var t = 0; t < temp.length; ++t )
{
    element[t] = temp[t].split(",");
}

Mildly sneaky. Works so long as none of the substrings contain semicolons.
Seems hardly worth trouble. I'd just use version 1.

korssane 01-28-2013 01:05 PM

Thanks man.

I will try this.

jmrker 01-29-2013 04:25 AM

To 'OldPedant':

Please pardon the interruption.
I have been experimenting with you script of post #2.
I easily figured out how to expand it to 3 (or more) elements within the array.
Very helpful.

However, to test my understanding of the DOM nomemclature, I tried to expand it
to use named elements, which you can find in the 3rd (non-working) attempt below.

The error I'm getting is that "TypeError: rec is not a constructor"
I've tried some variations on the code, but without success.

Could you tell me the correct syntax to use to make it a constructor?
Thank you for any time invested into my question.
Code:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title> Untitled </title>
<script type="text/javascript">

</script>

<style type="text/css">

</style>
</head>
<body>

<script type="text/javascript">
// Modified from: http://www.codingforums.com/showthread.php?t=286561

var s = "id1,name1,id2,name2,id3,name3...."
var temp = s.split(",");
var element = [ ];
for ( var t=0, e=0; t<temp.length; t+=2, ++e ) { element[e] = [ temp[t], temp[t+1] ]; }
alert(element.join('\n'));

// another test with 3 elements per record
var s = "id1,name1,phone1,id2,name2,phone2,id3,name3,phone3"
var temp = s.split(",");
var element = [ ];
for ( var e=0, t=0; t<temp.length; t+=3, ++e ) { element[e] = [ temp[t], temp[t+1], temp[t+2] ]; }
alert(element.join('\n'));

// still another test using DOM to define 4 elements per record
// this version does NOT work correctly

var rec = { id:'', name:'', state:'', phone:'' };
var s = "id1,name1,state1,phone1,id2,name2,state2,phone2,id3,name3,state3,phone3"
var tarr = s.split(",");
var elem = [ ];
for ( var e=0, t=0; t<temp.length; t+=4, ++e ) {
  elem[e] = new rec;  // error seems to point to this line.
  elem[e].id = tarr[t];
  elem[e].name=tarr[t+1]
  elem[e].state=tarr[t+2];
  elem[e].phone=tarr[t+3];
}
var str = '';
for (var i=0; i<element.length; i++) {
  str += elem[i].id+','+elem[i]+','+elem[i].state+','+elem[i].phone+'<br>';
} document.write(str);

</script>

</body>
</html>


rnd me 01-29-2013 05:25 AM

jmrker: this should fix your problem
Code:

var rec =function(){return { id:'', name:'', state:'', phone:'' }; };

Philip M 01-29-2013 07:58 AM

And another fix

str += elem[i].id+','+elem[i].name+','+elem[i].state+','+elem[i].phone+'<br>';

jmrker 01-29-2013 06:04 PM

Quote:

Originally Posted by rnd me (Post 1309388)
jmrker: this should fix your problem
Code:

var rec =function(){return { id:'', name:'', state:'', phone:'' }; };

Quote:

Originally Posted by Philip M (Post 1309402)
And another fix

str += elem[i].id+','+elem[i].name+','+elem[i].state+','+elem[i].phone+'<br>';

Thank you both. First for the function :thumbsup: and second for the typo which I could not see without the function. :o


All times are GMT +1. The time now is 11:45 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.