1

How to convert "{'label' = [1, 2]}" to table in lua? Answer expected: {'label' = [1,2]}.

James Z
  • 12,209
  • 10
  • 24
  • 44
  • 1
    `{'label' = [1,2]}` is not a valid Lua syntax – Egor Skriptunoff Feb 04 '22 at 13:15
  • 2
    `{['label'] = {1,2}}` or simply `{label = {1,2}}`. – lhf Feb 04 '22 at 13:40
  • Why do you state "Answer expected"? did you not try that had see it did not work? also since it is invalid syntax why did you expect it? – Nifim Feb 04 '22 at 15:36
  • Sorry, my bad Exact issue is - basically i am reading .mat file from python (because while reading the file from lua using matio i was getting "segmentation fault") and then passing that data to lua in the string form. Something like this - "{'header': b'MATLAB 5.0 MAT-file Platform: posix, Created on: Thu Feb 3 18:29:31 2022', 'version': '1.0', 'globals': [], 'train_patch': array([[[[-2.44379087e-02, 8.37826949e-02, -2.50160102e-02], [ 8.32045934e-02, -2.55941117e-02, -4.53651835e-02], [ 7.29143864e-02, -4.53651835e-02, -3.56530781e-02]]]]), 'train_labels': array([[6, 1, 8, ..., 2, 5, 0]])}" – SAURAV DAGAR Feb 07 '22 at 05:20
  • But while converting this data into table using load(), i am getting error as Error - test.lua:56: bad argument #1 to 'load' (function expected, got nil) stack traceback: [C]: in function 'load' test.lua:56: in main chunk [C]: in function 'dofile' ...user/torch/install/lib/luarocks/rocks/trepl/scm-1/bin/th:150: in main chunk [C]: in ? – SAURAV DAGAR Feb 07 '22 at 05:20

1 Answers1

0

A string full of Lua Code can be "converted" with load().
Example to play with in an interactive Lua console...

> _VERSION
Lua 5.4
> tab = 'return {label = {1, 2}}' -- A string for load()
> load(tab)()
table: 0x565fc800
> load(tab)().label
table: 0x565fcd00
> load(tab)().label[1]
1
> load(tab)().label[2]
2
koyaanisqatsi
  • 2,585
  • 2
  • 8
  • 15
  • Hi, thanks for the reply. – SAURAV DAGAR Feb 07 '22 at 05:15
  • basically i am reading .mat file from python (because while reading the file from lua using matio i was getting "segmentation fault") and then passing that data to lua in the string form. Something like this - "{'__header__': b'MATLAB 5.0 MAT-file Platform: posix, Created on: Thu Feb 3 18:29:31 2022', '__version__': '1.0', '__globals__': [], 'train_patch': array([[[[-2.44379087e-02, 8.37826949e-02, -2.50160102e-02], [ 8.32045934e-02, -2.55941117e-02, -4.53651835e-02], [ 7.29143864e-02, -4.53651835e-02, -3.56530781e-02]]]]), 'train_labels': array([[6, 1, 8, ..., 2, 5, 0]])}" – SAURAV DAGAR Feb 07 '22 at 05:18
  • But while converting this data into table using load(), i am getting error as Error - test.lua:56: bad argument #1 to 'load' (function expected, got nil) stack traceback: [C]: in function 'load' test.lua:56: in main chunk [C]: in function 'dofile' ...user/torch/install/lib/luarocks/rocks/trepl/scm-1/bin/th:150: in main chunk [C]: in ? – SAURAV DAGAR Feb 07 '22 at 05:19
  • Lua 5.1? - Than use ```loadstring()``` – koyaanisqatsi Feb 07 '22 at 07:54
  • It is giving nil output on using loadstring. – SAURAV DAGAR Feb 07 '22 at 10:58
  • Check it out in an interactive Lua 5.1 console: ```print(table.concat(loadstring('return {1, 2}')(), '\n'))``` – koyaanisqatsi Feb 07 '22 at 17:28
  • Or: ```tab = loadstring('return {label = {1, 2}}')() print(table.concat(tab.label, '\n'))``` – koyaanisqatsi Feb 07 '22 at 18:34
  • Thanks for your help so far. But still not working on my input, ie, {'header': b'MATLAB 5.0 MAT-file Platform: posix, Created on: Thu Feb 3 18:29:31 2022', 'version': '1.0', 'globals': [], 'train_patch': array([[[[-2.44379087e-02, 8.37826949e-02, -2.50160102e-02], [ 8.32045934e-02, -2.55941117e-02, -4.53651835e-02], [ 7.29143864e-02, -4.53651835e-02, -3.56530781e-02]]]]), 'train_labels': array([[6, 1, 8, ..., 2, 5, 0]])}. Is there some way to read .mat files in lua. Using matio to read files is giving error. Actual code - https://github.com/hbutsuak95/BASS-Net/blob/master/bass-net_model.lua – SAURAV DAGAR Feb 08 '22 at 04:09
  • Thats not a table. Thats looking like json. You need something to convert this format to a Lua table. In Lua itself there exists a nice string method you could use for this: ```string.gsub()``` - Or look how others handle it: https://stackoverflow.com/questions/24908199/convert-json-string-to-lua-table – koyaanisqatsi Feb 08 '22 at 10:27