Feature Overview
This is a script that parses String data and converts it into a hash table.
- a single string can initialize many values.
- returns at least an empty table.
- elements can be added space-separated.
- spaces can be replaced with
\t
. - newline codes are ignored.
- newline can be replaced with
\r
. {}
specifies a table.key=val;
key{val}
specifies the key and value of the table.- if you enclose an empty string, the element will become nil.
- to initialize with an empty string, use
/0/
. - bool values, and hexadecimal strings are automatically normalized.
- the first layer can be initialized with global values by using
cat()
in_env
. - the replacement search argument only supports one character.
- the replacement string can be of any length.
{}
=;
\n
(space)
cannot be used as a replacement search character because it is an internal reserved word.- instead, you can use
{}
=;
\n
(space)
as the replacement character. - this code requires the
replace()
function. - This function consumes 156( +61 for
replace()
) Token.
may want to Use DMP() if check the converted table.
This function is included in the KNUTIL library.
Sample code
-- Create a basic array, but you can use split() instead. table=htbl("1 2 3 4 5 6 pico 8") -- {1, 2, 3, 4, 5, 6, "pico", 8} |
-- Creates an associative array of key-value pairs. player=htbl("x=64;y=96;life=8;name=alex;") -- {x=64, y=96, life=8, name="alex"} |
-- Create a two-level array. mapspr=htbl("{1 2 3 4} {8 8 8 8} {5 6 7 8} {9 9 9 9}") -- {{1, 2, 3, 4},{8, 8, 8, 8},{5, 6, 7, 8} {9, 9, 9, 9}} |
-- Create a named array. jobs=htbl("class1{fighter mage cleric archer} class2{knight summoner priest ranger}") -- {class1={"fighter","mage","cleric","archer"}, class2={"knight","summoner","priest","ranger"}} |
Sample value format
Table
htbl"" -- {} htbl"{}" -- {{}} htbl"a=10;" -- {a=10} htbl"tbl{}" -- {tbl={}} |
Value
htbl"1" -- { 1 } htbl"0x10" -- { 16 } htbl"0b101" -- { 5 } htbl"str" -- {"str"} htbl"true false" -- {true, false} htbl"{}{/0/}{\t}{\r}" -- {{}, {""}, {" "}, {"\n"}} |
Sample replacement arguments
htbl(str, search, replace, [search, replace, ...])
htbl("\t is space") -- {" ", "is", "space"} htbl("\t is tab","\t","[tab]") -- {"[tab]", "is", "tab"} t=htbl("/0/ \b","\b","") -- {"", ""} -- #t[1]==0 #t[2]==0 |
Application Examples
You can initialize the first layer as a global value using cat() of _ENV
.
Init with 5tokens.
-- When testing with code you need to turn on 'puny font mode'.(ctrl-p) cat(_ENV,htbl[[cartname=cardgame; version=0.05; auther=unagi;]]) -- cartname="cardgame" -- version=0.05 -- auther="unagi" |
If the string is long you can use [[]]
to format the lines
htbl[[ rects{ rmtitlo{-1 2 130 16} rmtitli{-1 3 130 14} } pals{ pmono{017$ 007$ 11c$ 33b$ 449$ 228$ 55a$ 66d$ eef$} logop{1267c 00000$ 00010$ 00151$ 4086e 1059d 77777$ 3ba79$ 1467c} } centerfreq=130.813; centerkey=25; ]] |
UPDATE history



I updated with less code.
- code update saved token:
- htbl 7 tokens
- tonorm 9 tokens



I updated to HTBL() ver 0.4!!
v0.4
- Cut token costs (-12 tokens)
- Tonorm() is now built-in. the second return value of the final result is now nil.(be careful when doing add())
- Document inserted.



trying to use this...
in htbl(ht,c)
is ht
the string? and c
... what is c
?
I want to input a string and get back an associative array like:
mytbl=htbl("x=100,y=200,z=300") ?mytbl.y --200 |
what is the syntax?



@camp39
Thanks for trying! To create the associative array you described, use the following code:
mytbl=htbl("x=100;y=200;z=300;") |
The argument ht
to htbl(ht,c)
is a string, but there is no need to enter anything in c
since this function processes recursively.



ah, the last one needs a semicolon as well. got it. pretty cool, but it looks like this function can do a lot more. A blog post spelling it all out would be of interest to someone like me... a lot of util carts I cant make heads or tails of without examples and plain english.



@camp39
Ah, you're absolutely right.
This function can do a lot of things, but the explanations were not very appropriate! (I'm sorry if the English is hard to understand because I've been translating it a lot...)
I may have been too focused on creating the code and the cart.
I think I need to add some sample code for basic and advanced use.
Addendum: I have revised the post. I removed some unimportant statements and added sample code.
Please let me know if you have any questions or concerns.



I updated to HTBL() ver 0.5!!
v0.5
- Removed internal-only arguments and made them global variables.
- This means that the second return value no longer has any effect.
- To create an empty string, just specify
/0/
as the value. - You can use the tab character
\t
to replace it with a space character. - You do not need to specify
nil
to empty the contents of a table; an empty string will result innil
. \r
will be replaced with\n
.- (Result -1 tokens for these update.)
- Added single character replacement option
htblp()
.
v0.5a
- fixed
htblp()
replacement arguments to be valid during recursive processing.



I updated to htbl() ver 0.5b!!
v0.5b
- will use the
htblp()
function as the mainhtbl()
.
htbl()
incorporates the replace()
functionality of htblp()
, but htbl()
remains compatible with v0.5a.
[Please log in to post a comment]