From: Sietse Brouwer <sbbrouwer@gmail.com>
To: mailing list for ConTeXt users <ntg-context@ntg.nl>
Subject: Parsing [key=value, key2=value2, ...] with Lua
Date: Thu, 27 Sep 2012 18:42:24 +0200 [thread overview]
Message-ID: <CAF=dkzziA5uoyjq5eFaHO2bmBWUhkWTB55BDcTbXMcQzrVx+yw@mail.gmail.com> (raw)
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
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!
maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage : http://www.pragma-ade.nl / http://tex.aanhet.net
archive : http://foundry.supelec.fr/projects/contextrev/
wiki : http://contextgarden.net
___________________________________________________________________________________
reply other threads:[~2012-09-27 16:42 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CAF=dkzziA5uoyjq5eFaHO2bmBWUhkWTB55BDcTbXMcQzrVx+yw@mail.gmail.com' \
--to=sbbrouwer@gmail.com \
--cc=ntg-context@ntg.nl \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).