Get Text Between Quotes [JavaScript]

  • Hacker007
  • Proficient
  • Proficient
  • User avatar
  • Posts: 371
  • Loc: Riverside, CA

Post 3+ Months Ago

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)
Code: [ Select ]
--------- ACTIVE EVENTS ----------

[MACH-01] = "0000001"
[MACH-02] = "0000002"
[MACH-03] = "0000003"
  1. --------- ACTIVE EVENTS ----------
  2. [MACH-01] = "0000001"
  3. [MACH-02] = "0000002"
  4. [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
Code: [ Select ]
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;
}
  1. function getActiveEvents() {
  2.     //Just trust me, the line below loads "ActiveEventsLog.dbf" as a String
  3.     var ActiveEventsLog = queryActiveEvents();
  4.     
  5.     //This object is what I return at the end..
  6.     var ActiveEvents = new Object();
  7.     
  8.     //Turn the string into an array.. each new line is one array element
  9.     ActiveEventsLog = ActiveEventsLog.split("\r\n");
  10.     
  11.     //Loop through each array element (each line of original file)
  12.     for ( d = 0; d < ActiveEventsLog.length; d++ )
  13.     {
  14.         //If it's a Key/Value Pair line
  15.         if ( ActiveEventsLog[d].charAt(0) == "[" )
  16.         {
  17.             //Next 5 Lines Rips out the [] in the "Key"
  18.             l = "";
  19.             l = ActiveEventsLog[d];
  20.             l = l.split("]");
  21.             l = l[0];
  22.             l = l.replace("[", "");
  23.             //Next 8 Lines Rips out the QUOTES and SPACES in the "Value"
  24.             k = "";
  25.             k = ActiveEventsLog[d];
  26.             k = k.split("=");
  27.             k = k[1];
  28.             k = k.replace("\"", "");
  29.             k = k.replace("\"", "");
  30.             k = k.replace(" ", "");
  31.             k = k.replace(" ", "");
  32.             
  33.             //If BOTH Key AND Value are present, add it to the return variable
  34.             if ( ( l != "" ) & ( k != "") )
  35.             {
  36.                 ActiveEvents[''+l+''] = k;
  37.             }
  38.         }
  39.     }
  40.     
  41.     //Return our Associative Array(Object)
  42.     return ActiveEvents;
  43. }





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!
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • ScottG
  • Proficient
  • Proficient
  • ScottG
  • Posts: 448

Post 3+ Months Ago

You could turn the string of information into JSON and then use a json decode function to turn into an array (object)

I quickly made this to turn your example text into json
JAVASCRIPT Code: [ Select ]
function text_to_json(text) {
   
   // Turn [ and ] into "
   text = text.split('[').join('"');
   text = text.split(']').join('"');
   
   // Turn = into :
   text = text.split('=').join(':');
   
   // Break on hard space
   text = text.split("\r\n");
   
   // Join on a comma
   text = text.join(',');
   
   // Wrap in { }
   text = '{' + text + '}';
   
   // Return text
   return text;
   
}
 
// Your test info
var text = '[MACH-01] = "0000001"' + "\r\n" + '[MACH-02] = "0000002"' + "\r\n" + '[MACH-03] = "0000003"';
 
// Object like assoc array
var array_object = JSON.parse(text_to_json(text));
 
 
  1. function text_to_json(text) {
  2.    
  3.    // Turn [ and ] into "
  4.    text = text.split('[').join('"');
  5.    text = text.split(']').join('"');
  6.    
  7.    // Turn = into :
  8.    text = text.split('=').join(':');
  9.    
  10.    // Break on hard space
  11.    text = text.split("\r\n");
  12.    
  13.    // Join on a comma
  14.    text = text.join(',');
  15.    
  16.    // Wrap in { }
  17.    text = '{' + text + '}';
  18.    
  19.    // Return text
  20.    return text;
  21.    
  22. }
  23.  
  24. // Your test info
  25. var text = '[MACH-01] = "0000001"' + "\r\n" + '[MACH-02] = "0000002"' + "\r\n" + '[MACH-03] = "0000003"';
  26.  
  27. // Object like assoc array
  28. var array_object = JSON.parse(text_to_json(text));
  29.  
  30.  


Most browsers support JSON.parse(); for others you can use this script. https://github.com/douglascrockford/JSO ... r/json2.js

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 100 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.