i just started with lua and i got this problem where i have to read this file 4a
ID: 3591223 • Letter: I
Question
i just started with lua and i got this problem where i have to read this file
4a. Reading in the file
Read the file into an array of tables, then sort the table by Area of Research
Sort the table alphabetically by the area of research the award was given for. Print the results in some neat form.
The output should look something like this, for example:
4b. Sort the table by Institution of Winner,
Sort the table alphabetically by the institution of the winner. Print the results in some neat form.
The output should look something like this:
The file looks like this.
1982 Stephen Cook University of Toronto Algorithms
the problem gave us a hint saying we are supposed to modify this lua code.
i noticed that for each column there is a tab so i think we supposed to splice it after each tab i.e
{1966", "Alan Perlis", "Yale University", "Compilers"}
thank you in advance
1966 Alan Perlis Yale University Compilers 1967 Maurice Wilkes University of Cambridge Hardware 1968 Richard Hamming Bell Telephone Laboratories Coding Systems 1969 Marvin Minsky MIT AI 1970 James Wilkinson National Physical Laboratory Numerical Analysis 1971 John McCarthy MIT AI 1972 Edsger Dijkstra Eindhoven University of Technology Programming Languages 1973 Charles Bachman General Electric Databases 1974 Donald Knuth Stanford University Algorithms 1975 Allen Newell Carnegie-Mellon University AI 1975 Herbert Simon Carnegie-Mellon University AI 1976 Michael Rabin Hebrew University Automata Theory 1976 Dana Scott University of Oxford Automata Theory 1977 John Backus IBM Programming Languages 1978 Robert Floyd Stanford University Programming Languages 1979 Kenneth Iverson IBM Programming Languages 1980 Charles Hoare University of Oxford Programming Languages 1981 Edgar Codd IBM Databases1982 Stephen Cook University of Toronto Algorithms
the problem gave us a hint saying we are supposed to modify this lua code.
i noticed that for each column there is a tab so i think we supposed to splice it after each tab i.e
{1966", "Alan Perlis", "Yale University", "Compilers"}
thank you in advance
function fromCSV (s) s = s .. ',' -- ending comma local t = {} -- table to collect fields local fieldstart = 1 repeat -- next field is quoted? (start with `"'?) if string.find(s, '^"', fieldstart) then local a, c local i = fieldstart repeat -- find closing quote a, i, c = string.find(s, '"("?)', i+1) until c ~= '"' -- quote not followed by quote? if not i then error('unmatched "') end local f = string.sub(s, fieldstart+1, i-1) table.insert(t, (string.gsub(f, '""', '"'))) fieldstart = string.find(s, ',', i) + 1 else -- unquoted; find next comma local nexti = string.find(s, ',', fieldstart) table.insert(t, string.sub(s, fieldstart, nexti-1)) fieldstart = nexti + 1 end until fieldstart > string.len(s) return t end t = fromCSV('"hello "" hello", "",""') for i, s in ipairs(t) do print(i, s) end --> 1 hello " hello --> 2 "" --> 3
Explanation / Answer
Suppose our file is a text file.
data = {}
local file1 = io.open("InputFile.txt", "r")
for line in file1 :lines() do
data [#data + 1]= line:match("^(%d+)+(%a+)%s+(%a+)%s+(%a+)%s$")
end
-- you can use table.insert also for inserting data into a table then sort. Below is the example here only 2 columns are shown
--for line in io.lines(file1) do
-- local w, n = string.match(line, "^(%w+)"), string.match(line, "(%w+)$")
-- table.insert(data, {w, n})
--end
-- for sorting on subject
local sort_fnc = function( a,b ) return a.data[4]< b.data[4] end
table.sort( data, sort_func )
-- for sorting on institution
local sort_fnc1 = function( a,b ) return a.data[3]< b.data[3] end
table.sort( data, sort_func1 )
-- This part is for just printing the output data
for i = 1, #data do
year = data [i][1] -- Get year from array for checking further
name = data [i][2] -- Get Name from array for checking further
print(data [i][1].." ".. data[i][2].." ".. data[i][3] .." ".. data[i][4]);
end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.