Skip to content

Commit

Permalink
Remove empty fields after split to ensure fields match what's expected.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ethan Brown committed Jun 11, 2013
1 parent e794565 commit 9c06e2a
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion jawk.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,17 @@ function executeAction( rule, context ) {
}
}

// taken from http://stackoverflow.com/questions/281264/remove-empty-elements-from-an-array-in-javascript
Array.prototype.clean = function(deleteValue) {
for (var i = 0; i < this.length; i++) {
if (this[i] == deleteValue) {
this.splice(i, 1);
i--;
}
}
return this;
};

if( begin ) executeAction( begin, jawkContext );

readlines( inputFname, 'utf8', function(line) {
Expand All @@ -100,7 +111,7 @@ readlines( inputFname, 'utf8', function(line) {
if( line.match( rules[i].regex ) ) {
jawkContext.$0 = line;
// build record fields
var fields = line.split( new RegExp( jawkContext.RS ) );
var fields = line.split( new RegExp( jawkContext.RS ) ).clean('');
jawkContext.NR = fields.length;
var j;
for( j=0; j<fields.length; j++ ) jawkContext['$'+(j+1)] = fields[j];
Expand Down

0 comments on commit 9c06e2a

Please sign in to comment.