How To: Delete columns of data

Peter M. Brigham pmbrig at gmail.com
Fri Sep 5 10:50:30 EDT 2014


On Sep 5, 2014, at 7:59 AM, FlexibleLearning.com wrote:

> The routine lets you specify any number of columns to delete. You do not
> have to use it on each column. If you have 11 columns, simply specify which
> ones you do not want. If you have comma-separated data, you need to specify
> the columnDelimiter. This includes that option...

Here's a version of your handler that includes the option of specifying a column range to delete. I converted your handler to a function, it's more intuitive that way, for me at least. Call it like this:

put deleteColumns(tTable,"3-6") into tNewTable
-- will delete columns 3, 4, 5, and 6 of tTable (tab delimited)

----------

function deleteColumns pData,pColsToDelete,pDelim
   -- delete specified columns from a table
   -- Syntax: deleteColumns <data>,<cols>[,<delim>]
   --     data: Specifies the data to parse.
   --     cols: A comma separated list of columns to be removed,
   --         for example "2,5,7"
   --         or a column range: "3-5"
   --     delim: Optional column separator, for example "," or "|"
   --         if unspecified, defaults to tab.
   -- based on a handler by Hugh Senior, Use-LC list
   
   -- requires getItem()
   
   if pColsToDelete = empty then return pData
   if "-" is in pColsToDelete then
      put getItem(pColsToDelete,1,"-") into firstColNbr
      put getItem(pColsToDelete,2,"-") into lastColNbr
      repeat with i = firstColNbr to lastColNbr
         put i & comma after pColsToDelete
      end repeat
      delete char -1 of pColsToDelete
   end if
   if pDelim = empty then put tab into pDelim
   set the columnDelimiter to pDelim
   split pData by column
   put item 2 of extents(pData) into tMax
   repeat with n=1 to tMax
      if n is NOT among the items of pColsToDelete then
         add 1 to x
         put pData[n] into pData[x]
      end if
   end repeat
   repeat with n=x+1 to tMax
      delete local pData[n]
   end repeat
   combine pData by column
   return pData
end deleteColumns

function getItem tList,tIndex,tDelim
   -- returns item # tIndex of tList, given itemdelimiter = tDelim
   -- could just "get item tIndex of tList" in the calling handler but
   --    then have to set and restore the itemDelimiter, so this is less hassle
   -- defaults to tDelim = comma
   
   if tDelim = empty then put comma into tDelim
   set the itemdelimiter to tDelim
   return item tIndex of tList
end getItem

----------

-- Peter

Peter M. Brigham
pmbrig at gmail.com
http://home.comcast.net/~pmbrig





More information about the use-livecode mailing list