Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

script problem with reading in table

Status
Not open for further replies.

theEclipse

Programmer
Dec 27, 1999
1,190
US
I have this function:<br><br>function readIn(){<br>// the variables.<br>var table=new Array();<br>var row=-1,col=-1,rloop=true,cloop=true;<br><br>//this variable, str holds the table's original html throughout the function<br>var str=document.all.theTable.innerHTML.toLowerCase();<br><br>//edited is the string that gets edited in the function.<br>var edited=str<br><br>//remove all of the new line, and carrage return characters<br>var reg=/(\r)¦(\n)/g<br>str=str.replace(reg,'');<br><br>//loop through the rows of the table<br>while (rloop){<br>&nbsp;&nbsp;row++;<br><br>//make the current slot of the table array contain an array.<br>//(sort of an on the fly 2-d array maker)<br>&nbsp;&nbsp;table[row]=new Array();<br><br>//delete everything before the end of the first &lt;tr&gt; tag<br>&nbsp;&nbsp;edited=edited.substring(edited.indexOf('&lt;tr&gt;')+4,edited.length);<br><br>//loop through each cell in the current row<br>&nbsp;&nbsp;while (cloop){<br>&nbsp;&nbsp;&nbsp;&nbsp;col++;<br><br>//delete everything before the end of the next &lt;td&gt; tag<br>&nbsp;&nbsp;&nbsp;&nbsp;edited=edited.substring(edited.indexOf('&lt;td&gt;')+4,edited.length);<br><br>//put the current cells data in the coresponding array slot<br>&nbsp;&nbsp;&nbsp;&nbsp;table[row][col]=edited.substring(0,edited.indexOf('&lt;/td&gt;'));<br><br>//delete everything before the end of the next &lt;/td&gt; tag (probably not necessary)<br>&nbsp;&nbsp;&nbsp;&nbsp;edited=edited.substring(edited.indexOf('&lt;/td&gt;')+5,edited.length);<br><br>//if we have reached the end of the row, exit the inner loop<br>&nbsp;&nbsp;&nbsp;&nbsp;if (edited.indexOf('&lt;td&gt;')&gt;edited.indexOf('&lt;/tr&gt;') ¦¦ edited.indexOf('&lt;td&gt;')==-1) {cloop=false}<br>&nbsp;&nbsp;&nbsp;&nbsp;}//the end of the inner loop (for the cells)<br><br>//delete everything before the end of the next &lt;/tr&gt; tag (also probably not necessary)<br>&nbsp;&nbsp;edited=edited.substring(edited.indexOf('&lt;/tr&gt;')+5,edited.length);<br><br>//if we have reached the end of the table, dont run the loop again, otherwise, make sure the inner loop runs again.<br>&nbsp;&nbsp;if (edited.indexOf('&lt;tr&gt;')==-1) {rloop=false} else {cloop=true}<br><br>}// the end of the outer while loop<br><br>//the next 10 lines are just to make a string from the array and print it out to the webpage.<br>var str='&lt;table border=2&gt;';<br>for (k=0; k&lt;table.length; k++){<br>str+='&lt;tr&gt;';<br>for (j=0; j&lt;table[k].length; j++){<br>str+='&lt;td&gt;'+table[k][j]+'&lt;/td&gt;';<br>}<br>str+='&lt;/tr&gt;';<br>}<br>str+='&lt;/table&gt;';<br>document.write(str);<br><br>}//the end of the function.<br><br><br>the only problem with the function is that when it is reading in the table, it adds cells. for example, if you have a 2*3 table, when the loops read in this table you get something like this:<br><br>----------------<br>¦cell0-1¦cell0-2¦<br>---------------------------------<br>¦-------¦-------¦cell1-1¦cell1-2¦<br>-------------------------------------------------<br>¦-------¦-------¦-------¦-------¦cell2-1¦cell2-2¦<br>-------------------------------------------------<br><br>except the cells with dashes are just undefined.<br><br>can somebody please helllllppppp? <p>theEclipse<br><a href=mailto:eclipse_web@hotmail.com>eclipse_web@hotmail.com</a><br><a href=robacarp.webjump.com>robacarp.webjump.com</a><br>**-Trying to build a documentation of a Javascript DOM, crossbrowser, of course. E-mail me if you know of any little known events and/or methods, etc.
 
I figured it out. When the script went to reset the cloop variable (to make the inner loop run next time) it also needed to reset the col variable.<br><br>geeze thats twice in one day! <p>theEclipse<br><a href=mailto:eclipse_web@hotmail.com>eclipse_web@hotmail.com</a><br><a href=robacarp.webjump.com>robacarp.webjump.com</a><br>**-Trying to build a documentation of a Javascript DOM, crossbrowser, of course. E-mail me if you know of any little known events and/or methods, etc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top