This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
asto63's profile picture

GetLine(), does it exist?

Started by asto63, 07 July 2015 - 12:53 PM
asto63 #1
Posted 07 July 2015 - 02:53 PM
Hello, i have a code, but i want to get a specific line of a http.request…. But how plz ?
Does exist something like GetLine() for get a specific line ?

Thanks for futur help.
KingofGamesYami #2
Posted 07 July 2015 - 02:55 PM
Well, http requests return a string… which can be manipulated.


local function getLine( str, n )
  local i = 1
  for line in str:gmatch( "[^\r\n]+" ) do --#matches things that are not newline characters
    if i == n then --#if we're on the line you wanted
      return line --#return it
    end
    i = i + 1
  end
end
MKlegoman357 #3
Posted 07 July 2015 - 03:02 PM
Well, http requests return a string… which can be manipulated.

Nope, they return a table similar to the one returned by a call to fs.open() in read mode. So it's not 'GetLine()' but rather 'readLine()'. Do note that 'readLine()' does not read a specific line, rather reading the file/response line by line. You could for example put all the lines into a table and then index that table to get a specific line:


local lines = {}

for line in response.readLine do
  lines[#lines + 1] = line
end

print("The second line says:")
print(lines[2])
KingofGamesYami #4
Posted 07 July 2015 - 04:32 PM
Sorry, I meant the content is a string, and therefor can be manipulated via string operators.

Both solutions work, mine wants the entire string (as returned by response.readAll), whereas your solution will want the handle (if it was made into a function).
asto63 #5
Posted 08 July 2015 - 03:48 PM
Thank u KingOfGamesYami.