lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


It was thus said that the Great marbux once stated:
> Hi, All,
> 
> I'm working on an autoreplace script and am hoping for a tip that
> might get me past a problem in unpredictability of key names. (Users
> enter key/value pairs in a GUI to build a table of strings that will
> replace other strings.)
> 
> Because the order in which Lua returns non-array keys is
> unpredictable, this type of substitution is problematic. For example,
> if Lua returns the key for the en dash (two hyphens) before the key
> for the em dash (three hyphens), the script will produce instead of an
> em dash an en dash trailed by a hyphen.
> 
> So my question is how I can assure that when multiple abbreviations
> share the same leading sequence of identical characters, the keys are
> processed in longest to shortest order?  (I don't anticipate any
> problems if all keys were processed in longest to shortest order.)

  You could do something like this:

	list = {}
	repeat
	  local key,value = get_next_substitution()
	  list[key] = value
	  list[#list + 1] = key
	until done
	table.sort(list,function(a,b) return #a > #b end

  At the end, the array would look like:

	list =
	{
	  ["---"] = "—",
	  ["--"] = "–",
	  ["sss"] = "§",
	  ["ssss"] = "§§",
	  ["ppp"] = "¶",
	  ["pppp"] = "¶¶",
	  [1] = "pppp",
	  [2] = "ssss",
	  [3] = "---",
	  [4] = "ppp",
	  [5] = "sss",
	  [6] = '--'
	}

  With that:

	function Replace_Substrings_in_Strings(s,tSubs)
	  for i = 1 , #tSubs do
	    s = s:gsub(tSubs[i],tSubs[tSubs[i]])
	  end
	  return s
	end

  I also did something similar in LPeg, which goes through the string once. 
I describe it here:

	http://boston.conman.org/2013/01/14.1

  -spc