https://github.com/ravijo/dump-lua-array
Dump 1-dimensional Lua array into a file efficiently
https://github.com/ravijo/dump-lua-array
lua tutorial
Last synced: 9 months ago
JSON representation
Dump 1-dimensional Lua array into a file efficiently
- Host: GitHub
- URL: https://github.com/ravijo/dump-lua-array
- Owner: ravijo
- License: mit
- Created: 2018-07-10T15:23:44.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2020-01-15T13:40:47.000Z (about 6 years ago)
- Last Synced: 2025-03-27T04:42:26.327Z (about 1 year ago)
- Topics: lua, tutorial
- Language: Lua
- Homepage: https://ravijo.github.io/lua/
- Size: 8.79 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# dump-lua-array
Dump 1-dimensional Lua array into a file efficiently
Environmental Details
---------------------
1. Ubuntu 14.04 LTS 64 Bit OS
1. Lua v5.1
1. GCC v4.8.4
Install Lua v5.1
-----------------
1. Open terminal or press CTRL+ALT+T then use following command-
```
sudo apt-get install lua5.1
```
2. Install Lua headers by following command-
```
sudo apt-get install liblua5.1-0-dev
```
Compilation
-------------
Before proceding further make sure to download the code from the repository.
1. Open terminal or press CTRL+ALT+T
1. Compile C code using the following command-
```
gcc -I/usr/include/lua5.1 -o array.so -shared array.c -fPIC
```
The above command is going to produce shared object.
Read files
-------------
1. Compile C code using the following command-
```
gcc -o read read.c
```
2. Execute `read` from terminal
Usage
-----
We can use the above shared object in Lua code as shown below-
```
-- add the shared object to our Lua code
-- make sure to keep 'array.so' along with this code
require "array"
-- define number of elements in our array
local n = 1000000
-- create array
local my_array = {}
for i=1,n do
my_array[i] = i+1
end
-- now we have an array called 'my_array',
-- which we want to dump into a file. there
-- are two ways to do it
-- case 1
-- create new array first
-- then call dump function
local arr = array.new(my_array)
array.dump(arr, "out.bin")
-- case 2
-- directly call dump function
array.dump(my_array, "out.bin")
-- if we use 'array.new', we can do much more as shown below
local i=1
print(string.format("size of arr:%d, my_array[%d]:%d, arr[%d]:%d", array.size(arr), i, my_array[i], i, array.get_element(arr, i)))
```
Note
----
Check out my blog post [here](https://ravijo.github.io/lua/) for more information about it.
Issues (or Error Reporting)
----
Please check [here](https://github.com/ravijo/dump-lua-array/issues) and create issues accordingly.