Thread: iLiad Lua: scripting for Iliad
View Single Post
Old 12-13-2008, 07:30 AM   #3
hansel
JSR FFD2
hansel can extract oil from cheesehansel can extract oil from cheesehansel can extract oil from cheesehansel can extract oil from cheesehansel can extract oil from cheesehansel can extract oil from cheesehansel can extract oil from cheesehansel can extract oil from cheese
 
hansel's Avatar
 
Posts: 305
Karma: 1045
Join Date: Aug 2008
Location: Rotterdam, Netherlands, Europe, Sol 3
Device: iliad
Hi ericshliao,

Quote:
Originally Posted by ericshliao View Post
The Lua website declares that it's some script for game (if I read it correctly). Do we have some example to run on iLiad?
No useful examples yet... But I was able to fetch a web page and read the Iliad networkprofiles (XML) with just a few lines of code...

Lua is not only for games: it's a full blown general purpose language.
You can do anything you want with files, numbers, regular expressions, sockets, whatever. It's used in games because it's small, fast, and easy to embed. String handling is much easier than in C. You don't have to compile: you can test on your PC, and run on your Iliad by just copying the file. Plus it can do what you normally do with shell scripts...

I'm still learning Lua (used to C, C++ and Ruby), but I like what I see!
Here's a small example with file io and (cryptic ) string stuff: you can save it as hexdump.lua
Code:
local index = 0       -- offset in file
local block = 16      -- bytes on a line

-- process file if given, else read from stdin
if arg[1] then
  io.input(arg[1])
end

while true do
  local bytes = io.read(block)
  if not bytes then break end
  io.write(string.format("%04X:  ", index))               -- byte counter
  for b in string.gfind(bytes, ".") do
    io.write(string.format("%02X ", string.byte(b)))    -- hex values
  end
  io.write(string.rep("   ", block - string.len(bytes) + 1))
  io.write(string.gsub(bytes, "%c", "."), "\n")            -- ascii values
  index = index+block
end
You can call is as:
Code:
echo "hello" | lua hexdump.lua   # read from standard input
lua hexdump.lua /etc/passwd         # read a file and dump it
lua hexdump.lua </etc/passwd       # use file content as input
Google around a bit and be happy ;-)

Last edited by hansel; 12-15-2008 at 04:59 AM. Reason: correct version of example
hansel is offline   Reply With Quote