Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>phg already provided a nice LPeg solution for your question but here's another one using LPeg's re module. The syntax is closer to BNF and the operators used are more 'regex' like so this solution may be easier to grok.</p> <pre><code>re = require 're' function dump(t) io.write '{' for _, v in ipairs(t) do io.write(v, ',') end io.write '}\n' end local textformat = [[ data_in &lt;- block+ block &lt;- text '[' block_content ']' block_content &lt;- {| data_arr |} / (block / text)* data_arr &lt;- (text ':' nums whitesp)+ text &lt;- whitesp [%w' ']+ whitesp nums &lt;- (' '+ {digits} -&gt; tonumber)+ digits &lt;- %d+ whitesp &lt;- %s* ]] local parser = re.compile(textformat, {tonumber = tonumber}) local arr1, arr2 = parser:match(data) dump(arr1) dump(arr2) </code></pre> <p>Each block of data array gets captured into a separate table and returned as one of the outputs by <code>match</code>.</p> <p>With <code>data</code> being the same input as above, two blocks are matched and captured and so 2 tables are returned. Inspecting these two tables gives:</p> <blockquote> <p>{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,9,343,3938,9433,8756,6270,4472,3182,2503, 1768,1140,836,496,326,273,349,269,144,121,94,82,64,80,66,59,56,47,50,46,64,35,42 ,53,42,40,41,34,35,41,39,39,47,30,30,39,12345,} {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,212,3079,8890,8941,6177,4359,3625,2420, 1639,974,594,438,323,286,318,296,206,132,96,85,65,73,62,53,47,55,49,52,29,44,44, 41,43,36,50,36,40,30,29,40,35,30,25,31,47,31,25,29,24,30,35,31,28,31,17,37,35,30 ,20,33,28,20,37,25,21,23,25,36,27,35,22,23,15,24,34,28,}</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload