Advertisement
PaymentOption

SplitCSV

Sep 10th, 2012
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.62 KB | None | 0 0
  1. function SplitCSV_ToTable( sCSV )
  2.     -- There is a first comma and then a last.
  3.     local tValues = {}
  4.     local nLastComma_Pos_Dupe = 1
  5.     local nLastComma_Pos = 1
  6.     local nValueLength = 0 -- This will be the difference in comma positions.
  7.    
  8.     local bFirstRun = true
  9.     local bEndOfString = false
  10.     local cEndCharacter = string.sub( sCSV, string.len( sCSV ), string.len( sCSV ) ) -- Get the last character in the list.
  11.    
  12.     while not bEndOfString do
  13.         nLastComma_Pos_Dupe = nLastComma_Pos
  14.         -- test,test
  15.         nLastComma_Pos = string.find( sCSV, ",", nLastComma_Pos+1, string.len( sCSV ) )
  16.        
  17.         if nLastComma_Pos ~= nil then
  18.             nValueLength = nLastComma_Pos - nLastComma_Pos_Dupe
  19.            
  20.             if bFirstRun then
  21.                 -- Handle a single space.
  22.                 if string.sub( sCSV, 1, 1 ) == " " then
  23.                     tValues[1] = string.sub( sCSV, 2, nValueLength )
  24.                 else
  25.                     tValues[1] = string.sub( sCSV, 1, nValueLength )
  26.                 end
  27.                
  28.                 bFirstRun = false
  29.             else
  30.                 if string.sub( sCSV, nLastComma_Pos_Dupe+1, nLastComma_Pos_Dupe+1 ) == " " then
  31.                     tValues[#tValues+1] = string.sub( sCSV, nLastComma_Pos_Dupe+2, nValueLength + nLastComma_Pos_Dupe-1 )
  32.                 else
  33.                     tValues[#tValues+1] = string.sub( sCSV, nLastComma_Pos_Dupe+1, nValueLength + nLastComma_Pos_Dupe-1 )
  34.                 end
  35.             end
  36.         else
  37.             -- Get the last character.
  38.             if string.sub( sCSV, nLastComma_Pos_Dupe+1, nLastComma_Pos_Dupe+1 ) == " " then
  39.                 tValues[#tValues+1] = string.sub( sCSV, nLastComma_Pos_Dupe+2, string.len( sCSV ) )
  40.             else
  41.                 tValues[#tValues+1] = string.sub( sCSV, nLastComma_Pos_Dupe+1, string.len( sCSV ) )
  42.             end
  43.            
  44.             bEndOfString = true
  45.             return tValues
  46.         end
  47.     end
  48. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement