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
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:
1975 Allen Newell Carnegie-Mellon University AI 1971 John McCarthy MIT AI 2011 Judea Pearl UCLA AI 1969 Marvin Minsky MIT AI 1975 Herbert Simon Carnegie-Mellon University AI
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:
1983 Kenneth Thompson Bell Telephone Laboratories Operating Systems 1968 Richard Hamming Bell Telephone Laboratories Coding Systems 1983 Dennis Ritchie Bell Telephone Laboratories Operating Systems 2004 Robert Kahn CNRI Internet 2007 Joseph Sifakis CNRS Model Checking
The file looks like this.
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 Databases | |
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 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
|
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started