aboutsummaryrefslogtreecommitdiff
path: root/challenge-098/paulo-custodio/lua/ch-1.lua
blob: 90124c6509fac06636040904f19ded0a3ff60067 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env lua

--[[
Challenge 098

TASK #1 � Read N-characters
Submitted by: Mohammad S Anwar
You are given file $FILE.

Create subroutine readN($FILE, $number) returns the first n-characters and
moves the pointer to the (n+1)th character.

Example:
Input: Suppose the file (input.txt) contains "1234567890"
Output:
    print readN("input.txt", 4); # returns "1234"
    print readN("input.txt", 4); # returns "5678"
    print readN("input.txt", 4); # returns "90"
--]]

files = {}

function readN(filename, n)
    local file = files[filename]

    if file == nil then         -- file not yet open
        file = io.open(filename, "r")
        files[filename] = file
    end
    local text = ""
    for i=1,n do
        local c = file:read(1)
        if c ~= nil then
            text = text..c
        end
    end
    return text
end

for i=1, #arg-1, 2 do
    text = readN(arg[i], arg[i+1])
    io.write(text, "\n")
end