0

I should note that I am working with server side javascript. I have a text file on the local host in the format of:

"bla","foo"
"ble","faa"
"blu","foo"
...

I need to read that file in and compare the keys to a variable, and pull out the value if its found. Here is what I have right now:

var controlvar = 'blu';
var tbS = "C:\Users\me\list.txt";
var blaList = {};
var reader = new FileReader(tbS);
reader.onload = function(event) {
    var contents = event.target.result
    var lines = contents.split('\n');
    var elements = lines.split(',');
    blaList[elements[0]] = elements[1];
};
reader.onerror = function(event) {
    console.error("File could not be read! Code " + event.target.error.code)
};
for (var key in blaList) {
    if (/$controlvar/.test(key)) {
        set(blaList[key], Host);
    }
}
  • 1
    "with server side javascript" — What sort of server side JavaScript? NodeJS? Classic ASP? Netscape Livewire?! – Quentin Mar 23 '16 at 22:53
  • "Here is what I have right now" — So you have a vaguely defined platform, and you have some code. Do you have a question? – Quentin Mar 23 '16 at 22:53
  • The `onload` function runs asynchronously, so the `for` loop will run before the reader fills in `blaList`. – Barmar Mar 23 '16 at 22:54
  • Seconding what Quentin said, you should add that as a tag so your question gets more attention from people able to answer it. Also, I think you're trying to read the file into a map, not a hash - but I might just not be familiar with your technology's glossary. – Aaron Mar 23 '16 at 22:54
  • One issue I see is you're iterating over the "blaList" outside of the onload event. Also you can't have a variable inside the regex literal. Use "new RegExp()" to include variables. – Miguel Mota Mar 23 '16 at 22:55
  • Also, `/$controlvar/` doesn't do what you think. Variables aren't expanded inside Regexp literals. You need to do `var regex = new RegExp(controlvar);` – Barmar Mar 23 '16 at 22:55

0 Answers0