SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict
    Join Date
    Apr 2005
    Posts
    396
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Matrix assignment assigns values to entire row

    I have a weird issue that I'm pretty sure is a basic misunderstanding of multidimensional arrays in javascript.

    Here's the minimal test case:

    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    	<title>Title</title>
    	<script type="text/javascript">
    	    var s 		= new Array (10);
    		var row 	= new Array (20);
    		for (var x = 0, xlen = s.length; x < xlen; x++)
    		{
    			for (var y = 0, ylen = row.length; y < ylen; y++)
    			{
    				row[y] = 0;
    			}
    			s[x] = row;
    		}
    		
    		s[1][19] = 1;
    		
    		var str = '';
            for(var j = 0, len = s.length; j < len; j++)
            {
                str += s[j] + '\n'; 
            }
            
            
            
            alert('Matrix: \n' + str);
    		
    	</script>
    </head>
    <body></body>
    </html>
    What I expect to happen is that the 0 at 1,19 will be replaced with a 1, and that will show in the alert. What actually happens is that every element with an index of 19 is set to 1, not just the one I set.

    Why is it doing that?
    Bring out our hope and reason, before we pine away.

  2. #2
    SitePoint Addict
    Join Date
    Apr 2005
    Posts
    396
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, I solved the problem, and I think I get why it did what it did.

    When I made row an array and assigned values to it, then I assigned row to s[...], what I was really doing was assigning the value to a pointer, as it were, and since s had 10 copies of the precisely same array (not just copies, but actually the same memory address), when I changed a value in one, it changed in them all, whereas getting the value worked fine, returning only one value.

    So I changed it, creating a new array during the outer loop, and now it works.

    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    	<title>Title</title>
    	<script type="text/javascript">
    	    var s 		= new Array (10);
    		for (var x = 0, xlen = s.length; x < xlen; x++)
    		{
    		    s[x] = new Array(20);
    			for (var y = 0, ylen = s[x].length; y < ylen; y++)
    			{
    				s[x][y] = 0;
    			}
    		}
    		
    		s[1][19] = 1;
    		
    		var str = '';
            for(var j = 0, len = s.length; j < len; j++)
            {
                str += s[j] + '\n'; 
            }
            
            alert('Matrix: \n' + str);
    		
    	</script>
    </head>
    <body></body>
    </html>
    Lately I've been banging my head on bugs, then asking for help about 10 minutes before I figure out what the issue was all along. Story of my life. Hope this helps someone!
    Bring out our hope and reason, before we pine away.


Bookmarks

Posting Permissions

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