extract a column of text

Richard Gaskin ambassador at fourthworld.com
Wed Sep 3 19:27:32 EDT 2008


-= JB =- wrote:
> What is the easiest way to extract a column of text from a variable.
> For instance if I have a variable that has 200 lines with 12 items
> in each line and I want to extract all 200 lines of item 4.
> 
> I have seen it done somewhere which allows me to either remove
> a column or extract a column but I do not remembe


I've benchmarked many methods, so far the simplest is also the fastest:

put GetCol( tData, 4) into tMyCol
-- where tData is simply tab-delimited text,
-- and 4 is the column number you want to get


function GetCol pData, pCol
   set the itemdel to tab
   put empty into tReturnList
   repeat for each line tLine in pData
      put item pCol of tLine &cr after tReturnList
   end repeat
   delete last char of tReturnList -- trailing CR
   return tReturnList
end GetCol


On my 2.16GHz machine, that performs at the rate of about 520,000 lines 
per second.  Hopefully it'll be fast enough for your needs. :)

Splitting it into an array is very useful if you then need to go back 
and extract a large number of specific elements.  But for sequential 
processing of an entire data set, I've not yet found a method which 
benchmarks faster than "repeat for each".

-- 
  Richard Gaskin
  Managing Editor, revJournal
  _______________________________________________________
  Rev tips, tutorials and more: http://www.revJournal.com



More information about the use-livecode mailing list