Bon, il a été longtemps depuis que j'ai posté ici, mais j'ai vraiment besoin d'aide ! Je suis travaillant sur une HTA et coder beaucoup de lui à l'aide de JavaScript. Une tâche au sein de l'Application consiste à lire un fichier et prendre des paires clé/valeur et les charger dans un tableau associatif. J'ai réalisé cela, mais je veux trouver un moyen plus efficace de le faire pour accélérer le processus et maintenir le code plus propre (moins encombré). Voici mon code :
ActiveEventsLog.dbf (c'est le fichier qui est lu)
--------- ACTIVE EVENTS ----------
[MACH-01] = "0000001"
[MACH-02] = "0000002"
[MACH-03] = "0000003"
- --------- ACTIVE EVENTS ----------
- [MACH-01] = "0000001"
- [MACH-02] = "0000002"
- [MACH-03] = "0000003"
Voici une partie de la.Fichier HTA (la fonction JavaScript qui transforme le contenu du fichier ActiveEventsLog.dbf (ci-dessus) dans un tableau associatif (ou plutôt un objet fonctionnant comme un tableau associatif--damn JS) j'ai jeté dans des commentaires détaillés pour votre 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;
- }
Donc, comme vous pouvez le voir, j'ai utiliser 8 lignes de code pour retirer l'Id d'événement (le numéro à 7 chiffres dans les citations de ActiveEventsLog.dbf) du fichier il fonctionne, mais je pense que fonctionne mieux une regex ou quelque chose, ou peut-être un replace_all type fonction (je dois remplacer chaque citation avec valeur null individuellement dès maintenant). J'espère que quelqu'un ici (est-ce joebert encore post ici??) peut me donner une idée, que je veux mon code comme léger et efficace que possible.
Merci beaucoup !
"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." -Martin Golding