View Full Version : Arrays of arrays...
jakbo
10-05-2005, 03:59 AM
Is what I'm doing the right approach to creating an array of arrays? I have an external datafile with several employees' records. They are stored in an array with each element of the array being the complete employee record, ie: name, date of hire, etc., etc. These attributes are delimited by a :. I'm trying to separate these attributes while keeping the original array intact. That way I can access these attributes and validate whether or not an instance of the employee object will occur. There are too many records to create this from the start, so I'm trying to implement a for loop to do it for me. This is what I tried: datafile is the name of the original array of employees. emp is the array I'm trying to create .
for(var i=0;i<datafile.length;i++){
var emp = datafile[i].split(":");
//document.writeln(emp.length+"<br>");
/document.writeln(datafile[i]+"<br>");
}
//document.writeln(datafile[0]+"<br>");
pccode
10-05-2005, 06:47 AM
Currently it isn't possible to read a text file with javascript. You can open a text file, but only to view it inside your web browser. For your script to work you would need to somehow assign each line of text in your text file to a new variable in an array. That's rather easy to do with php or perl, but unfortunately it's not possible with javascript. However if you are able to somehow create an array with each variable representing a line of text from your text file then you could use the following:
//each names variable represents a different line from your text file
var names = new Array(2);
names[0] = "jim:jack:harry:mike:gary:terrence";
names[1] = "sally:jane:joe:angela:boris:dude";
//loop through each variable in names array
for (j in names) {
//split names by : and assign each value to the individual array
var individual = new Array();
individual = names[j].split(":");
//print number of variables in individual array
document.write(individual.length + "<br>");
//loop through each variable in individual array and print
for (i in individual) {
document.write(individual[i] + "<br>");
}
//print entire record without splitting
document.write(names[j] + "<br><br>");
}
I may be misunderstanding what you're trying to do, but if you're just trying to split the records up so that they each appear on a new line, you can just as easily open up the text file in microsoft word and click on edit and then replace. Then enter ":" in the find box and in the replace box enter "^l". Or click in the replace box, then click the more button, then click the special button, then select manual line break. Then click replace all. Make sure you make a copy of the file before you replace anything since you stated above that you don't want to make any changes to the original. Just copy and paste it into another folder like "my documents" before you open it.
One other possibility is to convert your text file into an external javascript. Make a backup copy of it and rename the copy "data.js". Then open it up in Word. Click on tools and click autocorrect options. Uncheck all of the boxes listed on the autoformat, autocorrect, and "auto format as you type" tabs. Then click on edit, then click replace. In the find box enter "paragraph mark". You can find it listed on the special formatting menu. It's possible that your lines might end with a manual line break instead. Try it both ways. Then in the replace box type: ";^pnum++;^precords[num] = " (include the quotes). Then click replace all. Then go back to the first line and hit enter so that you have a blank first line. On the first line type: var num = 0;
Hit enter and type the following on the second line: var records = new Array();
Then go to the third line and fix the beginning of it so that it reflects all of the other lines. And check the very last line in the document and delete the entire line if it contains: records[num] = "
Don't allow Word to change any formatting. Make sure it doesn't capitalize anything or change the type of quotes. Then click save. Click yes if a box pops up.
Then copy the code below and paste it into notepad. Save it as test.htm
<html>
<head>
<script src="data.js" type="text/javascript"></script>
<script language="javascript">
//loop through each variable in records array
for (j in records) {
//split records by : and assign each value to the individual array
var individual = new Array();
individual = records[j].split(":");
//print number of variables in individual array
document.write(individual.length + "<br>");
//loop through each variable in individual array and print
for (i in individual) {
document.write(individual[i] + "<br>");
}
//print entire record without splitting
document.write(records[j] + "<br><br>");
}
</script>
</head>
<body>
</body>
</html>
Double click on test.htm to run the program.
jakbo
10-05-2005, 03:02 PM
Thanks for the reply, and for the help. I'm kind of stuck on this one so I'll just show you what I have thusfar. It's really not much, right now I'm just sort of playing around with it to see what I can do with an array of records like this. I'm trying to become an expert at operating on arrays. Anyways, here it is:
//Here is the external datafile.
//datafile.js
var datafile = new Array();
datafile.push("Smith,Mary:9/9/2001:sw:5000:24000");
datafile.push("Wilen, Bill:4/18/1994:ne:4000:26000");
datafile.push("Wang, Chon:7/1/1998:ne:6000:30000");
datafile.push("Normal, Abi:1/21/2000:nw:10000:25000");
datafile.push("Arnold, Sarah:10/16/1990:sw:2000:27000");
datafile.push("Lowe, Sam:1/13/1992:sw:2000:24500");
datafile.push("Brody, Louise:1/26/1998:ne:3000:28000");
datafile.push("Chee, James:4/15/2004:sw:3200:25000");
datafile.push("Webb, Jack:11/4/2002:nw:6500:32500");
datafile.push("Mann, Beth:11/4/2006:nw:6500:32500");
datafile.push(":11/4/2002:nw:6500:32500");
datafile.push("Nabo, Ned:8/4/2003:nw:7100:-30900");
datafile.push("Inderfurth, Apo:2/4/2000:xw:3100:9900");
//Here is where I'm trying to play with it.
//sales.html
<html>
<head>
<style type = "text/css">
table{font-family: verdana,arial,sans-serif;
font-size: .7em;}
</style>
<script src = "datafile.js" type = "text/javascript"></script>
<script src = "Employee.js" type = "text/javascript"></script>
<script src = "DateFormat.js" type = "text/javascript"></script>
<script type = "text/javascript">
//for(var i=0;i<datafile.length;i++){
//document.writeln(datafile[i]+"<br>");
//}//just wanted to see if this would really work for me
for(var i=0;i<datafile.length;i++){
}
//for(var i=0;i<datafile.length;i++){
//var emp = datafile[i].split(":");
//document.writeln(emp.length+"<br>");
//document.writeln(datafile[i]+"<br>");
//}
//document.writeln(datafile[0]+"<br>");
//var emps = new Array();
//function getUserInput(){
//Employee.getRecord(datafile);
//}
</script>
</head>
<body>
</body>
</html>
I tried to do a few things with it. I could probably modify datafile.js in such a way that each employee record is stored as an array itself, and then assign all of those arrays to a new array which contains all of them, but if I ever have a really huge amount of records to deal with, this approach would not be practical. I have the array of full records in sales.html. Each element is a collection of one employee's attributes. I need to be able to get to those attributes and validate them. I'm just not sure how to do that. Again, thanks for the reply and the help, I appreciate it.
vwphillips
10-05-2005, 06:15 PM
not sure I understand all that but
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<script language="JavaScript" type="text/javascript">
<!--
var datafile = new Array();
datafile.push("Smith,Mary:9/9/2001:sw:5000:24000");
datafile.push("Wilen, Bill:4/18/1994:ne:4000:26000");
datafile.push("Wang, Chon:7/1/1998:ne:6000:30000");
datafile.push("Normal, Abi:1/21/2000:nw:10000:25000");
datafile.push("Arnold, Sarah:10/16/1990:sw:2000:27000");
datafile.push("Lowe, Sam:1/13/1992:sw:2000:24500");
datafile.push("Brody, Louise:1/26/1998:ne:3000:28000");
datafile.push("Chee, James:4/15/2004:sw:3200:25000");
datafile.push("Webb, Jack:11/4/2002:nw:6500:32500");
datafile.push("Mann, Beth:11/4/2006:nw:6500:32500");
datafile.push(":11/4/2002:nw:6500:32500");
datafile.push("Nabo, Ned:8/4/2003:nw:7100:-30900");
datafile.push("Inderfurth, Apo:2/4/2000:xw:3100:9900");
var zxcAllAry=new Array();
function zxcDimensionAry(zxcary){
for (zxc0=0;zxc0<zxcary.length;zxc0++){
zxcAllAry[zxc0]=new Array();
zxcs=zxcary[zxc0].split(':');
for (zxc1=0;zxc1<zxcs.length;zxc1++){
zxcAllAry[zxc0][zxc1]=zxcs[zxc1];
}
}
for (zxcD=0;zxcD<zxcAllAry.length;zxcD++){
document.getElementById('demo').value+=zxcAllAry[zxcD]+'\n';
}
}
//-->
</script>
</head>
<body onload="zxcDimensionAry(datafile);">
<textarea id="demo" cols=60 rows=20 ></textarea>
</body>
</html>
jakbo
10-05-2005, 11:37 PM
Both methods worked perfectly! Thank you guys for the help, my day and week just got a whole lot better. Thanks for your patience.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.