Created
August 20, 2013 18:33
-
-
Save curtislacy/6285360 to your computer and use it in GitHub Desktop.
Simple node.js script that takes input, splits it into lines, and does something with it (removes/counts duplicates, in this case).
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
process.stdin.resume(); | |
process.stdin.setEncoding('utf8'); | |
var lineCounts = {}; | |
var previousFragment = ''; | |
process.stdin.on('data', function (chunk) { | |
var lines = chunk.split( '\n' ); | |
lines[0] = previousFragment + lines[0]; | |
previousFragment = lines.pop(); | |
lines.forEach( function( line ) { | |
if( lineCounts.hasOwnProperty( line )) | |
lineCounts[ line ] ++; | |
else | |
{ | |
lineCounts[ line ] = 1; | |
} | |
}); | |
}); | |
process.stdin.on( 'close', function() { | |
var total = 0; | |
var unique = 0; | |
for( var k in lineCounts ) | |
{ | |
if( lineCounts.hasOwnProperty( k )) | |
{ | |
total += lineCounts[ k ]; | |
unique ++; | |
console.log( k ); | |
} | |
} | |
/* console.log( 'Total lines: ' + total ); | |
console.log( 'Total unique lines: ' + unique ); | |
console.log( 'Number of occurances by line: ' ); | |
console.log( lineCounts );*/ | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment