Reputation: 23
I am in the process of creating a health monitor based on the output of a logfile.txt. I'm reading the entire log into an array, then working the array within a for loop.
var txtFile = new XMLHttpRequest();
txtFile.open("GET", "EarLog.txt", true);
txtFile.onreadystatechange = function() {
if (txtFile.readyState === 4 && txtFile.status === 200) {
allText = txtFile.responseText;
lines = txtFile.responseText.split("\n");
}
var i = 0;
var name;
var status;
for (i = 5; i < 31; i++) {
var res = lines[i].split(": ");
name = res[0]
status = res[1];
}
document.write("<br>" + name[1] + "<br>")
}
txtFile.send(null);
And here is the part of the log file that I am reading from.
"Checkout Started at 14:52:11.57 :: Fri 07/24/2015"
StarTeam 11.0 Command Line Interface, Build 11.0.0.82
Copyright (c) 2003-2009 Borland Software Corporation. All rights reserved.
Using ini file: C:\ProgramData\Borland\StarTeam\ConnectionManager.ini
Folder: deployment (working dir: C:\Deployment\EARS\FutureRelease)
servicing.ear: skipped
portal.ear: checked out
.
.
.
communications.ear: checked out
"Checkout Completed at 14:54:11.63 :: Fri 07/24/2015"
However, instead of name being populated with the name of each ear, it is being populated by each individual letter of the last line read, in this case communications.ear so when I try to write something like
document.write(name[10])
instead of displaying the tenth ear, it displays the tenth letter of the last ear read. The same issue also occurs for status. Basically I want to be able to call the variable name[1] and have it show the string "portal.ear" and call the variable status[1] and have it show the string "checked out", and so on.
Upvotes: 0
Views: 52
Reputation: 22247
The variables name
and status
are strings, not arrays. So when you try to access the nth element, you get the nth letter.
If you want them to be arrays, change:
var name;
var status;
to:
var name = [];
var status = [];
Then fill the arrays using the push method. Change:
name = res[0]
status = res[1];
to:
name.push(res[0]);
status.push(res[1]);
Then you can refer to the strings stored in the name/status arrays as name[10], status[23], etc.
Upvotes: 1
Reputation: 26075
Change the code block to:
var i=0;
var name = [];
var status = [];
for (i = 5; i < 31; i++) {
var res = lines[i].split(": ");
name.push(res[0]);
status.push(res[1]);
}
Basically, you are storing value in a variable not in array, so when you call name[10] it actually refers to String variable not String[].
Upvotes: 1