Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-28-2013, 04:05 AM   PM User | #1
korssane
Regular Coder

 
Join Date: Jun 2011
Posts: 148
Thanks: 18
Thanked 0 Times in 0 Posts
korssane is an unknown quantity at this point
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
korssane is offline   Reply With Quote
Old 01-28-2013, 04:38 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,556
Thanks: 62
Thanked 4,056 Times in 4,025 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 01-28-2013, 01:05 PM   PM User | #3
korssane
Regular Coder

 
Join Date: Jun 2011
Posts: 148
Thanks: 18
Thanked 0 Times in 0 Posts
korssane is an unknown quantity at this point
Thanks man.

I will try this.
korssane is offline   Reply With Quote
Old 01-29-2013, 04:25 AM   PM User | #4
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,797
Thanks: 30
Thanked 462 Times in 456 Posts
jmrker will become famous soon enough
Question

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>
jmrker is offline   Reply With Quote
Old 01-29-2013, 05:25 AM   PM User | #5
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,553
Thanks: 9
Thanked 480 Times in 463 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
jmrker: this should fix your problem
Code:
var rec =function(){return { id:'', name:'', state:'', phone:'' }; };
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:15.2% IE7:0.5% IE8:8.4% IE9:8.5% IE10:8.5%
rnd me is online now   Reply With Quote
Users who have thanked rnd me for this post:
jmrker (01-29-2013)
Old 01-29-2013, 07:58 AM   PM User | #6
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,102
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
And another fix

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

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Old 01-29-2013, 06:04 PM   PM User | #7
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,797
Thanks: 30
Thanked 462 Times in 456 Posts
jmrker will become famous soon enough
Thumbs up

Quote:
Originally Posted by rnd me View Post
jmrker: this should fix your problem
Code:
var rec =function(){return { id:'', name:'', state:'', phone:'' }; };
Quote:
Originally Posted by Philip M View Post
And another fix

str += elem[i].id+','+elem[i].name+','+elem[i].state+','+elem[i].phone+'<br>';
Thank you both. First for the function and second for the typo which I could not see without the function.
jmrker is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:58 PM.


Advertisement
Log in to turn off these ads.