Okay, it has been a long time since I have posted here, but I really need some help! I am working on an HTA and using JavaScript to code a lot of it. One task within the Application is to read in a file, and take Key/Value pairs and load them into an associative array. I have accomplished this, but I want to find a more effective way of doing this to speed up the process and keep the code more clean(less cluttered). Here is my code:
ActiveEventsLog.dbf (this is the file that gets read)
--------- ACTIVE EVENTS ----------
[MACH-01] = "0000001"
[MACH-02] = "0000002"
[MACH-03] = "0000003"
- --------- ACTIVE EVENTS ----------
- [MACH-01] = "0000001"
- [MACH-02] = "0000002"
- [MACH-03] = "0000003"
Here is part of the .HTA File (the JavaScript Function that turns the contents of the ActiveEventsLog.dbf file (above) into an associative array (or rather an object functioning as an Associative array-- damn JS) I threw in detailed comments for your convience
function getActiveEvents() {
//Just trust me, the line below loads "ActiveEventsLog.dbf" as a String
var ActiveEventsLog = queryActiveEvents();
//This object is what I return at the end..
var ActiveEvents = new Object();
//Turn the string into an array.. each new line is one array element
ActiveEventsLog = ActiveEventsLog.split("\r\n");
//Loop through each array element (each line of original file)
for ( d = 0; d < ActiveEventsLog.length; d++ )
{
//If it's a Key/Value Pair line
if ( ActiveEventsLog[d].charAt(0) == "[" )
{
//Next 5 Lines Rips out the [] in the "Key"
l = "";
l = ActiveEventsLog[d];
l = l.split("]");
l = l[0];
l = l.replace("[", "");
//Next 8 Lines Rips out the QUOTES and SPACES in the "Value"
k = "";
k = ActiveEventsLog[d];
k = k.split("=");
k = k[1];
k = k.replace("\"", "");
k = k.replace("\"", "");
k = k.replace(" ", "");
k = k.replace(" ", "");
//If BOTH Key AND Value are present, add it to the return variable
if ( ( l != "" ) & ( k != "") )
{
ActiveEvents[''+l+''] = k;
}
}
}
//Return our Associative Array(Object)
return ActiveEvents;
}
- function getActiveEvents() {
- //Just trust me, the line below loads "ActiveEventsLog.dbf" as a String
- var ActiveEventsLog = queryActiveEvents();
-
- //This object is what I return at the end..
- var ActiveEvents = new Object();
-
- //Turn the string into an array.. each new line is one array element
- ActiveEventsLog = ActiveEventsLog.split("\r\n");
-
- //Loop through each array element (each line of original file)
- for ( d = 0; d < ActiveEventsLog.length; d++ )
- {
- //If it's a Key/Value Pair line
- if ( ActiveEventsLog[d].charAt(0) == "[" )
- {
- //Next 5 Lines Rips out the [] in the "Key"
- l = "";
- l = ActiveEventsLog[d];
- l = l.split("]");
- l = l[0];
- l = l.replace("[", "");
- //Next 8 Lines Rips out the QUOTES and SPACES in the "Value"
- k = "";
- k = ActiveEventsLog[d];
- k = k.split("=");
- k = k[1];
- k = k.replace("\"", "");
- k = k.replace("\"", "");
- k = k.replace(" ", "");
- k = k.replace(" ", "");
-
- //If BOTH Key AND Value are present, add it to the return variable
- if ( ( l != "" ) & ( k != "") )
- {
- ActiveEvents[''+l+''] = k;
- }
- }
- }
-
- //Return our Associative Array(Object)
- return ActiveEvents;
- }
So as you can see, I use 8 Lines of code to pull out the Event Id from the file (the 7 digit number in quotes from ActiveEventsLog.dbf) It works, but I think a regex or something would work better, or maybe a replace_all type function (I have to replace each quote with null value individually right now). Hopefully somebody here (does joebert still post here??) can give me some insight, as I want my code as light and efficient as possible.
Many Thanks!
"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." -Martin Golding