How To: Delete columns of data

Michael Doub mikedoub at gmail.com
Fri Sep 5 14:45:51 EDT 2014


Opps, I left off the getItem function.   Here it is again for completeness:


> just to complete your library, here is a function that will extract the listed columns in the order that you 
> specify but it does not modify the original data.
> 
> -= Mike
> 

function extractColumns @pData,pColsToReturn,pDelim
   -- Extract specified columns from a table in order
   -- Syntax: extractColumns <data>,<cols>[,<delim>]
   --     data: Specifies the data to parse.
   --     cols: A comma separated list of columns or column ranges to be returned in order
   --         for example "2,7,5"
   --         or a accending column range: "3-5"
   --         of a decending column range: "5-3"
   --         or a combination "2,4-5,7,11-9"
   --     delim: Optional column separator for example "," or "|"
   --         if unspecified, defaults to tab.
   -- based on a handler by Hugh Senior and Peter M. Brigham, Use-LC list
   
   -- requires getItem()
   
   if pColsToReturn = empty then return pData
   repeat for each item pCol in pColsToReturn
      if "-" is in pCol then 
         put getItem(pCol,1,"-") into firstColNbr
         put getItem(pCol,2,"-") into lastColNbr
         if firstColNbr < lastColNbr then
            repeat with i = firstColNbr to lastColNbr
               put i & comma after pColsToReturnExpanded
            end repeat
         else
            repeat with i = firstColNbr down to lastColNbr
               put i & comma after pColsToReturnExpanded
            end repeat
         end if
      else
         put pCol & comma after pColsToReturnExpanded
      end if
   end repeat
   put char 1 to -2 of pColsToReturnExpanded into pColsToReturn
   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 for each item n in pColsToReturn
         add 1 to x
         put pData[n] into rData[x]
   end repeat
   combine rData by column
   return rData
end extractColumns

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



More information about the use-livecode mailing list