lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Until now, I have a hard time to find some Lua scripts on the Web, at least
outside of TecGraf.
As I am still a beginner, I am avid to see examples of Lua code, to see how
people resolved some problems.
I suppose the lack of generic scripts is because Lua is more an extension
language, therefore leading to code mostly aimed at given (proprietary)
applications instead of generic processing.

Well, perhaps sometime I will set up a Web page as a repository of my little
scripts. Until this time, I will send them here...
I suppose there are some other beginners in the list, that can be interested
by my experiments.

I have made two scripts today:
- one generic, processing a file, line by line, and writing output to
another file.
As I am a Windows guy, I am more inclined to give the filenames on the
command line instead of redirecting files in the standard input / output.
But of course, you can easily modify the scripts to accept < ifile >
outfile.

- the other script was written for somebody which get text files without
newlines, and wanted to split the big unique line in several smaller ones.
Note: I know this code is far from optimal, so if you have any idea on how
to improve them (without loosing too much clarity: it is mainly educational
code), feel free to share with us.

I think one point that can be improved is the scanning of the end of a chunk
in search of a space. Perhaps I could use strfind here, but I don't know how
to do the search from the end to the beginning. Setting init as -1 doesn't
seems to work. Oh, actually, it means len - init. Not obvious from the doc...
RegExp could be used for more efficient processing, but I don't see how...
So I stick with this Basic-like style of coding...

Mmm, my (Web based) e-mail provider doesn't seems to allow multiple
attachments... I will just paste the code in this message. It is small, it should be
OK.

---------- ProcessFile.lua ----------

-- Process a file

filenameIn  = (arg and arg[1]) or [[C:\tmp\ProcessFile.lua.in.txt]]
filenameOut = (arg and arg[2]) or [[C:\tmp\ProcessFile.lua.out.txt]]
i = 0

-- Lame default processing, replace with your code
function ProcessLine(line)
  if line then
    local output
    i = i + 1
    output = "line " .. i .. ": " .. line .. "\n"
-- OR:
--    write("line " .. i .. ": " .. line .. "\n")
    return output
  else
    return nil
  end
end

function ProcessFile()
  if readfrom(filenameIn) and writeto(filenameOut) then
    local processedLine
    -- Loop on the lines and process them
    repeat
      processedLine = ProcessLine(read())
      if processedLine then  -- ProcessLine returns nil if read line is nil
(EOF)
        write(processedLine)
      end
    until processedLine == nil
-- OR:
--    while (ProcessLine(read())) do
--      -- Do nothing, output is done in ProcessLine
--    end
    write"That's all folks!"
    readfrom()  -- Restore default reading
    writeto()    -- Restore default writing
  else
    print("Error in parameters: " .. filenameIn .. " or " .. filenameOut)
  end
end

ProcessFile()

---------- SplitLongLines.lua ----------

-- Process a file to split long lines at blanks

filenameIn  = (arg and arg[1]) or [[C:\tmp\SplitLongLines.lua.in.txt]]
filenameOut = (arg and arg[2]) or [[C:\tmp\SplitLongLines.lua.out.txt]]
cutAt   = (arg and arg[3] and tonumber(arg[3])) or 80
method = (arg and arg[4]) or "w"  -- Values: "w" (white space) or "r" (raw)
lineNb = 0  -- For debug purpose

-- Cut at a separator before the cutAt length, or at cutAt size if no
separator is found.
-- Return the remainder of the line.
function CutAtWhiteSpace(chunk)
  if chunk and chunk ~= "" then
    local len = min(strlen(chunk), cutAt)
--D--print("---" .. lineNb .. "-" .. "len: " .. len)
    local i, c, remainder
    local splitPos = 0
    -- Search a separator from the end of the chunk
    for i = len, 1, -1 do
      c = strsub(chunk, i, i)
      -- Separators = white space, tabulation, carriage return or line feed
      if c == ' ' or c == '\t' or c == '\r' or c == '\n' then
        splitPos = i
        break
      end
    end
    if splitPos ~= 0 then
--D--print("splitPos: " .. splitPos)
      -- Found, split here
      write(strsub(chunk, 1, splitPos - 1) .. "\n")
      remainder = strsub(chunk, splitPos + 1)
    else
      -- No separator found
--D--print("Dump")
      write(strsub(chunk, 1, cutAt) .. "\n")  -- Arbitrary cut at given size
      remainder = strsub(chunk, cutAt + 1)
    end
--D--print("remainder len: " .. strlen(remainder))
    return remainder
  else
    return nil  -- chunk is nil or an empty string: EOF
  end
end

-- Just dump the chunk, which has already the right size
function CutAtLength(chunk)
  if chunk and chunk ~= "" then
    write(chunk .. "\n")
    return ""
  else
    return nil
  end
end

if method == "r" then
  ProcessChunk = CutAtLength
else
  ProcessChunk = CutAtWhiteSpace
end

function ProcessFile()
  if readfrom(filenameIn) and writeto(filenameOut) then
    local chunk
    local remainder = ""
    -- Loop on the lines and process them
    chunk = read(cutAt)
    repeat
--D--lineNb = lineNb + 1
      if chunk and strlen(remainder) <= cutAt then
        -- Process remainder of previous line and current chunk
        remainder = ProcessChunk(remainder .. chunk)
        chunk = read(cutAt)
      else
        -- remainder growed too big, have to split it
        remainder = ProcessChunk(remainder)
      end
--D--print("%%" .. (remainder or "(nil)") .. "%% " .. lineNb)
    until not remainder
--D--write"That's all folks!"
    readfrom()  -- Restore default reading
    writeto()    -- Restore default writing
  else
    print("Error in parameters: " .. filenameIn .. " or " .. filenameOut)
  end
end

ProcessFile()

----- END -----
Beware of e-mail client word-wrapping...

I love Lua!

Regards.

-- 
--._.·´¯`·._.·´¯`·._.·´¯`·._.·´¯`·._.·´¯`·._.·´¯`·._.--
Philippe Lhoste (Paris -- France)
Professional programmer and amateur artist
http://jove.prohosting.com/~philho/
--´¯`·._.·´¯`·._.·´¯`·._.·´¯`·._.·´¯`·._.·´¯`·._.·´¯`--

Sent through GMX FreeMail - http://www.gmx.net