2009-11-02

Remove Column of Text

Great for Sports Agate

The following function is something I regularly use for removing a column of text in sports agate cleanup scripts. It is very generic. Pass it a string, a zero based column index, and the delimiter (usually a tab specified as '\t' ) and the function will remove that column.

Note, you would need to call this with something that can process all the paragraphs. As this will only handle one paragraph at a time unless you get real fancy with the calls.

Look for another function to call this one in an upcoming post.

//
function removeColumn ( orig, col, delimiter ) {
//-------------------------------------------------------------------------
//-- R E M O V E C O L U M N
//-------------------------------------------------------------------------
//-- Generic: Yes. Should work for any ECMAScript based languages such as
//-- ExtendScript and JavaScript.
//-------------------------------------------------------------------------
//-- Purpose: To remove a 'delimiter' separated 'col'umn of text from the
//-- 'orig'inal passed string.
//-------------------------------------------------------------------------
//-- Arguments: 3
//-- orig: A string which delimited columns
//-- col: A column number. This can be negative to work from the back
//-- delimiter: a string which is used to delimit columns
//-------------------------------------------------------------------------
//-- Calls: Nothing, but requires the .split(), .splice(), and .join()
//-------------------------------------------------------------------------
//-- Returns: The original string with the specified column removed.
//-------------------------------------------------------------------------
//-- Sample Use:
//~ removeColumn ( "a\tb\tc\td\t\e\tf", -1, '\t' ) ; // "a\tb\tc\td\t\e"
//~ removeColumn ( "a\tb\tc\td\t\e\tf", 2, '\t' ) ; // "a\tb\td\t\e\tf"
//-------------------------------------------------------------------------
//-- Notes: Works great with negative column indexes. -1 is the last column
//-- If removing the last column be careful if you need the text to
//-- retain the final return or line break as this will delete it.
//-------------------------------------------------------------------------
//-- Edited: 2009.11.01 by Jon S. Winters of electronic publishing support
//-- Originally an internal function of a larger function.
//-- eps@electronicpublishingsupport.com
//-------------------------------------------------------------------------
//-- break the string into an array using the passed delimiter
var sa = orig.split(delimiter) ;
//-- Call inbuilt splice method to remove the specified element of the array
sa.splice(col,1) ;
//-- Join the array into a string using the passed delimater.
return sa.join(delimiter) ;
} //-- end of internal function.
//

No comments:

Post a Comment