Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Jun 2011
    Posts
    148
    Thanks
    18
    Thanked 0 Times in 0 Posts

    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

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,567
    Thanks
    78
    Thanked 4,385 Times in 4,350 Posts
    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.

  • #3
    Regular Coder
    Join Date
    Jun 2011
    Posts
    148
    Thanks
    18
    Thanked 0 Times in 0 Posts
    Thanks man.

    I will try this.

  • #4
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,087
    Thanks
    38
    Thanked 498 Times in 492 Posts

    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>

  • #5
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,350
    Thanks
    11
    Thanked 589 Times in 570 Posts
    jmrker: this should fix your problem
    Code:
    var rec =function(){return { id:'', name:'', state:'', phone:'' }; };
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • Users who have thanked rnd me for this post:

    jmrker (01-29-2013)

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,027
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    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.

  • #7
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,087
    Thanks
    38
    Thanked 498 Times in 492 Posts

    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •