Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

What is an equivalent for "io:read()" in Roblox?

Quick one. I'm wondering what an equivalent for io:read() in Roblox is, for multi-line strings not files like in regular Lua.

Script:

function Read_PPM(Text)
	local data = Text:read("*line")
	if data ~= "P6" then 
		return nil 
	end
 
    repeat
        data = Text:read("*line")
    until string.find(data, "#") == nil
 
    local image = {}
    local size_x, size_y
 
    size_x = string.match(data, "%d+")
    size_y = string.match(data, "%s%d+")
 
    data = Text:read("*line")
    if tonumber(data) ~= 255 then return nil end
 
    for i = 1, size_x do
        image[i] = {}
    end
 
    for j = 1, size_y do
        for i = 1, size_x do
            image[i][j] = {string.byte(Text:read(1)), string.byte(Text:read(1)), string.byte(Text:read(1))}
        end
    end
 
    return image
end

1 comment

0

You could split the multi-line string by each new line "\n" and then loop through the table to get each line.

1 answer

you very very often don't need to read files in roblox, which makes the IO library literally useless, and that's why its disabled..

you can use regex(regular experessions, AKA, pattern matching) to mimic the io:read(num) or io:lines(), etc..

in other words, if your seeking to deepen your knowledge in the sense of becoming a better roblox scripter, then you can ignore the IO library completely..

besides reading the output stream, there's no other application for IO, you can read the output stream via LogService.MessageOut, and write to it via print()..

here are examples:

local str = [[
My name is Builderman,
I am a 65 years old man,
I live in great britain,
I am from Iraq.

I have 2 sons, 3 daughters, and 4 grandchildrens
]]

--reads each line of the text, an prints it prefixed with "this ->"
for match in str:gmatch("(.-)\n") do
    print("This ->", match)
end

print("All = ", str:match("(.+)")) --equivalent of `IO:read("a")`

print(str:match("(.+)") == str)--this should be true

6 comments

0

What string patterns should I use in place of io:read("*all") and or io:read("*line"). All of the string patterns I've heard of seem to be for single-line strings.

0

i think could use: str:match("(.-)\n") to match a line, and str:match(".+") for everything -which should just be same as str

0

i added some examples

0

So how could I do something like "repeat data = Text:read("*line") until string.find(data, "#") == nil"?

0

I added the script im trying to get to work in Roblox if that helps with context

0

not sure what ur trying to do, but i am sure there's a better way to do it.. as of right now, regular expressions are the way to go..

This discussion is preserved as it was. New answers, comments, and votes are closed.