Google

main
dspblog
cyclic_signals
FFT_interpolation
FFT_interpolation_how_does_it_work
FFT_smoothness_cyclic
discrete_time_reconstruction
discrete_time_reconstruction2
Nyquist_on_the_edge
FFT_delay_special_case
Fourier_reconstruction
pulse_and_Nyquist
FFT_complexError
matlabOctave
matlab_upsampling
matlab_downsampling
FFT_delay
FFT_filter_example
FFT_interpolation_example
FFT_bin_frequencies
fit_signal
FFT_peaksearch_audio_example
matlab_binary_readwrite
Octavesvg
C
FFTW_example
looprecord
SNR
SNR3
SNR_example_96kAudio
SNR_FFT_correlation_example
lua
luagpib
luasplit
luadump
mnoofltk
wxLuaDll
wxLua_loadAsDll
wxLua_HelloWorld
wxLua_simpleButton
wxLua_resourceManagement
wxLua_XMLparser
DSP
IQ_LO
IQ_LO_2
optimum_receiver
DSP_basics
sampleRateChange_terms
dirac_pulse
freqresp_s
zfilter_example
freqresp_z
freqresp_z_sign
misc
zero_forcing_equalizer_example
nonminphase_inverse
periodic_spectrum
lagrange_multipliers
Entropy
RC_chopper
TRex450_setup
EP100
EP100SE
EP100Gremlins
essential_spares
tail_rotor
motoradjustment
blade_balancing
blade_repair
GAUI_SAE12A
Walkera43


valid html (click to verify)



prevupnextdisable ads

Split function for lua

Summary

A function to split a string into smaller parts, using an arbitrary separator.
It uses pattern matching by default, unless true is given for the plain parameter.

Example

split=require('split')
split("one two three four five", "%s+")

returns
{"one", "two", "three", "four", "five"}

Download

here

Code

-- ##########################################################################
-- # split() function for lua
-- # Markus Nentwig 2007
-- # This code is in the public domain and provided without any warranty.
-- ##########################################################################

local split=
   function(text, sep, plain)
      local res={}
      local searchPos=1
      while true do
	 local matchStart, matchEnd=string.find(text, sep, searchPos, plain)
	 if matchStart and matchEnd >= matchStart then
	    -- insert string up to separator into result
	    table.insert(res, string.sub(text, searchPos, matchStart-1))
	    -- continue search after separator
	    searchPos=matchEnd+1
	 else
	    -- insert whole reminder as result
	    table.insert(res, string.sub(text, searchPos))
	    break
	 end
      end
      return res
   end

-- ##########################################################################
-- # Self test / examples
-- ##########################################################################
local verif=
   function(teststr, pat, plain, check)
      local res=split(teststr, pat, plain)
      res=table.concat(res, "*")
      assert(res==check, "split self test assertion failed")
   end

-- "|-" as separator. In pattern quoted via %
verif("one|-two|-three|-four|-five", "%|%-", false, "one*two*three*four*five")

-- leading/trailing/duplicate separators cause empty string
verif("|-one|-two|-three|-four|-|-five|-", "%|%-", false, "*one*two*three*four**five*")

-- separator using pattern
verif("one1two22three333four4444five", "%d+", false, "one*two*three*four*five")

-- Disable pattern match
verif("one1two22three333four4444five", "%d+", true, "one1two22three333four4444five")

-- Empty string
verif("", "%S+", false, "")

-- Empty pattern returns single entry
verif("abcdefg", "", false, "abcdefg")

-- Example: Split along whitespace
verif("one two  three   four    five", "%s+", false, "one*two*three*four*five")

return split


prevupnextdisable ads

© Markus Nentwig 2007-2008
The content of this page is provided without any warranty and may not be reproduced without permission.

Comments? Questions?

Please send me a mail! mnentwig@elisanet.fi