Bueno, ha sido mucho tiempo desde que he posteado aquí, pero realmente necesito ayuda! Estoy trabajando en una HTA y utiliza JavaScript para código mucho de ella. Una de las tareas dentro de la aplicación es leer un fichero y tomar pares clave/valor y cargarlos en una matriz asociativa. He logrado esto, pero quiero encontrar una forma más efectiva de hacer esto para acelerar el proceso y mantener el código más limpio (menos desordenado). Aquí está mi código:
ActiveEventsLog.dbf (este es el archivo que se lee)
--------- ACTIVE EVENTS ----------
[MACH-01] = "0000001"
[MACH-02] = "0000002"
[MACH-03] = "0000003"
- --------- ACTIVE EVENTS ----------
- [MACH-01] = "0000001"
- [MACH-02] = "0000002"
- [MACH-03] = "0000003"
Aquí es parte del.Archivo HTA (la función de JavaScript que convierte el contenido del archivo ActiveEventsLog.dbf (arriba) en una matriz asociativa (o más bien un objeto funciona como una matriz asociativa--maldito JS) tiró en comentarios detallados para su 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;
- }
Así como usted puede ver, utilizar 8 líneas de código para sacar el Id de evento desde el archivo (el número de 7 dígitos en citas de ActiveEventsLog.dbf) funciona, pero creo que funcionaría mejor regex o algo, o tal vez un replace_all tipo de función (tengo que reemplazar cada cotización con valor nulo individualmente ahora). Ojalá alguien aquí (hace joebert todavía post aquí??) me puede dar una idea, como quiero mi código como ligero y eficiente como sea posible.
¡ Muchas gracias!
"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." -Martin Golding