-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path07-02_sorted_lines_verifying_out.lua
More file actions
58 lines (49 loc) · 1.3 KB
/
07-02_sorted_lines_verifying_out.lua
File metadata and controls
58 lines (49 loc) · 1.3 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
function readFile(inputFile)
local openedFile = false
if inputFile ~= nil then
io.input(inputFile)
openedFile = true
end
local lines = {}
for line in io.lines() do
lines[#lines+1] = line
end
if openedFile then
io.input():close()
end
return lines
end
function sortLines(lines)
table.sort(lines)
end
function writeOutput(sortedLines, outputFile)
local openedFile = false
if outputFile ~= nil then
local f = io.open(outputFile, "r")
if f ~= nil then
f:close()
io.write(string.format("Output file already '%s' exists. Overwrite? [y/n]\n", outputFile))
stream = io.input(io.stdin)
local confirmation = string.lower(io.read("l"))
if confirmation == "n" then
return
elseif confirmation ~= "y" then
io.write("Invalid option. Only 'y' or 'n' are accepted")
return
end
end
openedFile = true
end
io.output(outputFile)
for _, line in ipairs(sortedLines) do
io.write(line, "\n")
end
if openedFile then
io.output():close()
end
end
inputFile = arg[1]
outputFile = arg[2]
lines = readFile(inputFile)
sortLines(lines)
writeOutput(lines, outputFile)