Undefined error when accessing 2-D array contents

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • manutd7
    New Member
    • Jun 2010
    • 8

    Undefined error when accessing 2-D array contents

    Hi,

    Every time I try to access the contents of the following array it returns that it is undefined. I have tried multiple variations on accessing the arrays syntax wise but get different errors every time.

    When I try to do it the way listed below I get the undefined error I talked about before.

    Code:

    Code:
    	var array = new Array();
    	var i, j, txt;
    	var str = new Array();
    
    	try{
    		array = document.tsv.read_in("u:\\test.txt");
    		for(i=0;i<array.length;i++)
    			str[i]=array[i];
    		for(i=0; i<array.length; i++)
    			array[i] = new Array();
    	}
    	catch(err)
    	{
    		txt="There was an error on this page.\n\n";
    		txt+="Error description: " + err.description + "\n\n";
    		txt+=err.stacktrace + "\n\n";
    		txt+=err.stack + "\n\n";
    		txt+="Click OK to continue.\n\n";
    		alert(txt);
    	}
    	
    	for(i=0; i<array.length; i++)
    	{
    		var str2 = str[i];
    		var temp = str2.split("\t");
    		for(j=0;j<temp.length;j++)
    		{
    			array[i][j] = temp[j];
    		}
    		document.write(array[0][0]);
    	}
    
    	for (i=1;i<array.length; i++)
    	{
    		document.write("<div class=\"silverheader\" id=\""+i+"\"><a href="+i+">"+array[i][0]+"</a></div>");
    		document.write("<div class=\"submenu\">");
    		for(j=0;j<temp.length;j++)
    			document.write(array[i][j]+"\t");
    		document.write("</div>");
    	}
    I realize the code above is ridiculously inefficient but I'm just trying to figure out what I'm doing wrong. Any suggestions?
    Attached Files
    Last edited by manutd7; Jun 10 '10, 02:18 PM. Reason: Added Screenshot of the error
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    line #6, you’re overwriting your array variable.

    Comment

    • manutd7
      New Member
      • Jun 2010
      • 8

      #3
      Originally posted by Dormilich
      line #6, you’re overwriting your array variable.
      That's my initializing line, the array has nothing in its contents before that point.

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        that doesn’t matter. whatever it has been before, after line #6 it’s something different.

        Comment

        • manutd7
          New Member
          • Jun 2010
          • 8

          #5
          Yea, that's what I was going for cause that's when the contents of the array are first filled. I should have probably said this in the first post but during my debugging attempts I accessed the contents of the array after line 6, and I got the correct output - that is the contents that I brought in with the read_in function. When I try and access the contents in line 30 I get undefined for all index values. So I doubt that line 6 is where my problem is.

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            do you have a test page, where I can look for myself?

            Comment

            • gits
              Recognized Expert Moderator Expert
              • May 2007
              • 5390

              #7
              simplyfied it a bit to test in firebug-console -> basically it seems to work:

              Code:
              var my_array = [];
              var str      = [];
              var i, j, txt;
              
               
              my_array = ['foo\t8\tbar', 'foo','foo','foo','foo','foo'];
              
              for (i = 0; i < my_array.length; i++) {
                  str[i]   = my_array[i];
                  my_array[i] = [];
              }
              
              for(i = 0; i < my_array.length; i++) {
                  var str2 = str[i];
                  var temp = str2.split("\t");
              
                  for(j = 0; j < temp.length; j++) {
                      console.log(temp[j]);
              
                      my_array[i][j] = temp[j];
              
                      console.log(my_array[i][j]);
                  }
              }
              
              console.log(my_array);
              how does the array initially look like?

              Comment

              • manutd7
                New Member
                • Jun 2010
                • 8

                #8
                gits,

                I have attached the result of the code that you pasted above which still gives me the same "undefined" response when I try to access the 2-d array. Initially, the array consists of a line from a tsv file that is parsed in using the java read_in function that I created.

                If I try to access the array using the syntax "array[i,j]" I get the problem where each interation of the outer for loop overwrites the same row of the 2-d array. Basically at the end I have a 2-d array that has the same info throughout. Anybody have any answers to why I'm having these problems?

                Comment

                • gits
                  Recognized Expert Moderator Expert
                  • May 2007
                  • 5390

                  #9
                  what the code ... in fact yours ... produce is the following for my_array:

                  Code:
                  [["foo", "8", "bar"], ["foo"], ["foo"], ["foo"], ["foo"], ["foo"]]
                  is that what you expect?

                  since you use temp which has the las split operation's result assigned at this time there might be undefined values since temp might have had different lengths ...

                  Comment

                  • manutd7
                    New Member
                    • Jun 2010
                    • 8

                    #10
                    Yea that's the output I expect but unfortunately don't get. The temp arrays all have the same length as I'm inputting from a tsv file that has the same number of columns for the entire document.

                    Could it be a problem with the way my compiler or javascript settings on my browser? Cause if it works for you guys then there shouldn't be a reason why it doesn't work for me.

                    Comment

                    • gits
                      Recognized Expert Moderator Expert
                      • May 2007
                      • 5390

                      #11
                      nope ... as Domilich said already ... from here it would be the best to have a testpage where we could have a look at. at least we would need to have a look at what you have in the array - could you post what it contains before you are trying to loop through it? the array initially would need to look like the one i posted in line 6 above.

                      Comment

                      • manutd7
                        New Member
                        • Jun 2010
                        • 8

                        #12
                        For now all I'm using is a test.tsv for development and here's the array looks before the split:

                        Project Name\tDue Date\tDescripti on of the Project\tPeople Working on it
                        Project 1\t5/10/2010\tlalal\t"J oe, Mary"
                        Project 2\t5/11/2010\tjs\t"May, Joe"
                        Project 3\t5/12/2010\tas\t"bill y, greg, andy"

                        I don't have a public test page yet so I can't really show you guys anything.

                        Comment

                        • gits
                          Recognized Expert Moderator Expert
                          • May 2007
                          • 5390

                          #13
                          did you try to confirm that this works? you might try to hardcode the array first like this:

                          Code:
                          array = [
                              'Project Name\tDue Date\tDescription of the Project\tPeople Working on it',
                              'Project 1\t5/10/2010\tlalal\t"Joe, Mary"',
                              'Project 2\t5/11/2010\tjs\t"May, Joe"',
                              'Project 3\t5/12/2010\tas\t"billy, greg, andy"'
                          ];
                          with that it should work?

                          Comment

                          • manutd7
                            New Member
                            • Jun 2010
                            • 8

                            #14
                            Yea, when I hard code it like that it seems to work...

                            But when I check the contents right after I import the lines from the tsv file they come out correct. Even after I invoke the split function its ok, so why would it break in that case?

                            Comment

                            • gits
                              Recognized Expert Moderator Expert
                              • May 2007
                              • 5390

                              #15
                              so the array isn't exactly like that created ... you should check your import ... might be that there is a hidden character at the end or in a line or a similar problem?

                              Comment

                              Working...