0

I'm trying to load in two txt files and compare the differences between the two. More specifically I'm looping through one file per line and comparing it to every line in another txt file.

For the most part everything is working but I have found that I'm only able to access the array within the lr.on('line') function. However I have declared the array in the global scope.

Here is the code:

var LineByLineReader = require('line-by-line');
var lr = new LineByLineReader('phones.txt');
var lr2 = new LineByLineReader('wor.txt');


var phoneArray = [];
var worArray = [];

lr.on('error', function(err){
   if(err){
       console.log("We have found the following error: " + err);
   }
});

lr2.on('error', function(err){
   if(err){
       console.log("We have found the following error: " + err);
   }
});
 
lr.on('line', function(line){
   phoneArray.push(line);
});

lr2.on('line', function(line){
   worArray.push(line);
});

for(var i = 0; i < phoneArray.length; i++){
        for(var x = 0; x < worArray.length; x++){
            if(array1[i] === array2[x]){
                console.log("Found Match: " + array2[x]);
            }
        }
    }

3
  • stackoverflow.com/help/mcve Commented May 11, 2016 at 2:03
  • Whare are array1[i] and array[2] inside the for loop?Where you define them?Maybe you mean phoneArray and worArray? Commented May 11, 2016 at 2:05
  • Check the order of execution, are you looping before it's finished appending lines? Is there some kind of callback from the ` LineByLineReader when it's complete? Commented May 11, 2016 at 3:33

1 Answer 1

1

Maybe you just forgot to use the right variables names inside your for loop? And you just need this? :

if(phoneArray[i] === worArray[x]){
                console.log("Found Match: " + worArray[x]);
Sign up to request clarification or add additional context in comments.

1 Comment

That was a mistake on my part.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.