1

I been given the a text file, and I want to convert it to an associative array so I can use a plugin and to convert the data into a table. The smartest way to do this I believe is to make it into an assoc. array, but everything i been trying gets me nowhere. Originally the file only have the IDs, not the names and yes I could manually erase the names and just used the numbers but it just seems counterproductive.

Here is the text file users.txt

Person_1    USERID_1
Person_2    USERID_2
Person_3    USERID_3
Person_4    USERID_4
Person_5    USERID_5
Person_6    USERID_6
Person_7    USERID_7
Person_8    USERID_8
Person_9    USERID_9
Person_10   USERID_10

Here is how my code was to read the original file that just had the numbers, users.js

$(document).ready( function() {
    var userArray = [];
    $.get('../users.txt', function(data){
        userArray = data.split('\n');
        console.log(userArray);
        var barChartData = {
             labels : ["User 1","User 2","User 3","User 4","User 5","User 6","User 7","User 8","User 9","User 10"],
             datasets : [
                {
                    label: "Number of CD's Bought",
                    fillColor : "rgba(0,0,0,0.5)",
                    strokeColor : "rgba(0,0,0,0.8)",
                    highlightFill: "rgba(222,222,222,0.75)",
                    highlightStroke: "rgba(222,222,222,1)",
                    data : [userArray[0],userArray[1],userArray[2],userArray[3],userArray[4],userArray[5],userArray[6],userArray[7],userArray[8],userArray[9]]
                }
            ]
        };

How can I update my code so I can have User[0][0] to be Person_1 and User[0][1] to be USERID_1 and so on for the other. I tried to replace the newline \n but it only does it in the first line and the remaining newlines are in the console. Any ideas?

Just as note I originally get and XML file from a Query as A Web Service but that one has been even more troublesome

Criatura Eterna
  • 273
  • 1
  • 3
  • 16
  • What are the delimiting characters between data elements? Is that white-space or tabs `\t` between `Person` and `USERID`? Are there new lines `\n` or carriage returns `\r` (or both) at the end of each line? – Brett Jun 25 '15 at 18:51
  • Visit this stackoverflow post on [JavaScript multidimensional arrays](http://stackoverflow.com/questions/7545641/javascript-multidimensional-array) it is very helpful! – Skam Jun 25 '15 at 18:53
  • Yes there is a tab between between `Person` and `USERID`. As for the new line I am not certain if is just `\n`, `\r` or both. – Criatura Eterna Jun 25 '15 at 18:54

1 Answers1

2

You could try this

var userArray =data.split('\n');
for(i=0;i<userArray .length;i++){
    var u=userArray [i];
    var t=[];
    var temp=u.split(/\s+/g);

    t.push(temp[0]);
    t.push(temp[1]);
    userArray [i]=t;
}

Here is a fiddle

depperm
  • 10,606
  • 4
  • 43
  • 67
  • I tried your approach but when I tried `t.push(temp[0]);` I get `P` isntead of `Person_1` and when I tried `t.push(temp[1]);` I get `e` instead of `USERID_1`, I think the issue there is the the file that is being read – Criatura Eterna Jun 25 '15 at 19:15
  • that means temp is not splitting correctly, could you see what temp has after initialized? – depperm Jun 25 '15 at 19:18
  • This is the console entry when i did `console.log('This is temp ' + temp);` each of the entries for each of the lines. `This is temp Person_1,USERID_1 This is temp Person_2,USERID_2 This is temp Person_3,USERID_3 This is temp Person_4,USERID_4 This is temp Person_5,USERID_5 This is temp Person_6,USERID_6 This is temp Person_7,USERID_7 This is temp Person_8,USERID_8 This is temp Person_9,USERID_9 This is temp Person_10,USERID_10` – Criatura Eterna Jun 25 '15 at 19:25
  • and `t.push(temp[0])` just pushes P? – depperm Jun 25 '15 at 19:28
  • I get this `This is t after t.push(temp[0]) Person_1` So its pushing `Person_1`?? – Criatura Eterna Jun 25 '15 at 19:38
  • There was an error in my code. This is the solution! Thank you so much @depperm! – Criatura Eterna Jun 25 '15 at 19:56