Created
September 7, 2012 12:07
-
-
Save rjyo/3665639 to your computer and use it in GitHub Desktop.
read from a text file and remove all duplicated rows
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'); | |
function readLines(input, func) { | |
var remaining = ''; | |
input.on('data', function(data) { | |
remaining += data; | |
var index = remaining.indexOf('\n'); | |
while (index > -1) { | |
var line = remaining.substring(0, index); | |
remaining = remaining.substring(index + 1); | |
func(line); | |
index = remaining.indexOf('\n'); | |
} | |
}); | |
input.on('end', function() { | |
if (remaining.length > 0) { | |
func(remaining); | |
} | |
var keys = Object.keys(mailList); | |
for (var index in keys) { | |
console.log(keys[index]); | |
} | |
}); | |
} | |
var mailList = {}; | |
function func(data) { | |
var key = data.toLowerCase().trim(); | |
if (!mailList.hasOwnProperty(key)) { | |
mailList[key] = 1; | |
} | |
} | |
var input = fs.createReadStream('input.txt'); | |
readLines(input, func); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment