Parsing [key=value, key2=value2, ...] with Lua
![](https://secure.gravatar.com/avatar/1930c667fbd3884fd581e95eb4d5b15c.jpg?s=120&d=mm&r=g)
Hi all, I've found a helper function that does this: utilities.parsers.settings_to_hash('ape=1, note=2, mice=3') --> { ["ape"] = 1, ["note"] = 2, ["mice"] = 3 } This makes me very happy. This function and its friends are stored under utilities.parsers, and defined in util-prs.lua; I've listed them below. Perhaps they'll be useful to somebody else, too. I should probably add this info to http://wiki.contextgarden.net/Commands_with_KeyVal_arguments, but dinner first. Cheers, Sietse First some examples of what the most important functions: utilities.parsers.settings_to_hash('ape=1, note=2, mice=3') --> { ["ape"] = 1, ["note"] = 2, ["mice"] = 3 } -- settings_to_hash takes an option 'strict', -- I assume that has to do with space-trimming. utilities.parsers.settings_to_array('ape, note=2, mice, ape') --> { "ape", "note=2", "mice", "ape" } utilities.parsers.settings_to_array('ape, note=2, mice, ape') --> { ["ape"]=true, ["note=2"]=true, ["mice"]=true,} Complete list of functions (and one table) in utilities.parsers: T.add_settings_to_array T.arguments_to_table T.array_to_string T.getparameters T.hash_to_string T.listitem T.make_settings_to_hash_pattern T.patterns : table: 0x9a67680 T.patterns.settings_to_array : userdata: 0x9a6c904 T.patterns.settings_to_hash_a : userdata: 0x9a6a694 T.patterns.settings_to_hash_b : userdata: 0x9a6ad6c T.patterns.settings_to_hash_c : userdata: 0x9a6b424 T.settings_to_array T.settings_to_hash T.settings_to_hash_strict T.settings_to_hash_tolerant T.settings_to_set T.simple_hash_to_string T.splitthousands
participants (1)
-
Sietse Brouwer