Jump to content




read a color out of a file


  • You cannot reply to this topic
14 replies to this topic

#1 Leon669

  • Members
  • 57 posts
  • LocationGermany

Posted 13 April 2015 - 04:25 PM

Hi

i have a file only the color red in it

FileColor

red

so now in another program i want to add this color like

fg = fs.open("loginSettins/version","r")
farbe = fg.readLine()
paintutils.drawPixel(1,5,colors.farbe)
fg.close()

it doesnt work what to do ?

Edited by Leon669, 13 April 2015 - 06:23 PM.


#2 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 13 April 2015 - 04:37 PM

You can't do that at all, you have to read through the contents of the file in sequence, and you only get to do so one time through. I believe the readLine() method works on the handles returned by http.get as well.

#3 flaghacker

  • Members
  • 655 posts

Posted 13 April 2015 - 04:42 PM

Readline reads the next line, so calling readline multiple times will read diffrent lines.

file = fs.open ("aFile", "r")

print (file.readLine ()) --first line
print (file.readLine ()) -- second line
print (file.readLine ()) --you get the pattern...
...
file.close ()

You can also use file.readAll () and split the text using string.split, I'm notgoing to put an example here becauze I'm on mobile right now.


#4 Leon669

  • Members
  • 57 posts
  • LocationGermany

Posted 13 April 2015 - 04:45 PM

View Postflaghacker, on 13 April 2015 - 04:42 PM, said:

Readline reads the next line, so calling readline multiple times will read diffrent lines.

file = fs.open ("aFile", "r")

print (file.readLine ()) --first line
print (file.readLine ()) -- second line
print (file.readLine ()) --you get the pattern...
...
file.close ()

You can also use file.readAll () and split the text using string.split, I'm notgoing to put an example here becauze I'm on mobile right now.

thx it work ;)

but how to read pastebin files ?

#5 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 13 April 2015 - 05:22 PM

local file = http.get( "URL_TO_RAW_PASTE_DATA" )
local data = file.readAll()
print( data )


#6 Anavrins

  • Members
  • 775 posts

Posted 13 April 2015 - 05:28 PM

You can get in the whole file into a table like this
local file = http.get("www.example.com")
local content = {}
for line in file.readLine do
  table.insert(content, line)
end
And get specific lines such as line 3 with print(content[3])
To read from pastebin, you would use it's raw link "http://pastebin.com/raw.php?i=pAsTeC0dE"

Edited by Anavrins, 13 April 2015 - 05:29 PM.


#7 Anavrins

  • Members
  • 775 posts

Posted 13 April 2015 - 06:51 PM

Could you not have made another topic altogether instead?

Anyways, colors.farbe does not exist in the colors api.
And since farbe is a string, you need to convert it into a number.
So paintutils.drawPixel(1,5, tonumber(farbe)) would be the way to go.

Edited by Anavrins, 13 April 2015 - 06:54 PM.


#8 Leon669

  • Members
  • 57 posts
  • LocationGermany

Posted 13 April 2015 - 07:19 PM

View PostAnavrins, on 13 April 2015 - 06:51 PM, said:

Could you not have made another topic altogether instead?

Anyways, colors.farbe does not exist in the colors api.
And since farbe is a string, you need to convert it into a number.
So paintutils.drawPixel(1,5, tonumber(farbe)) would be the way to go.

doenst work it always shows lightBlue

Code
Spoiler

Edited by Leon669, 13 April 2015 - 07:23 PM.


#9 valithor

  • Members
  • 1,053 posts

Posted 13 April 2015 - 09:54 PM

View PostLeon669, on 13 April 2015 - 07:19 PM, said:

View PostAnavrins, on 13 April 2015 - 06:51 PM, said:

Could you not have made another topic altogether instead?

Anyways, colors.farbe does not exist in the colors api.
And since farbe is a string, you need to convert it into a number.
So paintutils.drawPixel(1,5, tonumber(farbe)) would be the way to go.

doenst work it always shows lightBlue

Code
Spoiler


Your problem is when you read the file it is returning a string not the table entry colors.red points to.

To fix this you could do something like this.

color = "colors.red" -- this is what you are getting from the file
color = loadstring("return "..color)()


#10 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 13 April 2015 - 11:16 PM

I thought the file merely contained the word "red", in which case you'd do:

local fg = fs.open("loginSettins/version","r")
local farbe = fg.readLine()
paintutils.drawPixel(1,5,colors[farbe])
fg.close()


#11 jakejakey

  • Members
  • 98 posts

Posted 14 April 2015 - 01:49 AM

Please delete this post i realized that i had mispelled some things in my code

Edited by jakejakey, 14 April 2015 - 03:08 AM.


#12 HPWebcamAble

  • Members
  • 933 posts
  • LocationWeb Development

Posted 14 April 2015 - 02:07 AM

View Postjakejakey, on 14 April 2015 - 01:49 AM, said:

f = fs.open("file","r")
file = {}
i = 0
repeat
curLine = f.readLine()
if curLine == nil then
else
file[i] = curLine
end
until curLine == nil

Bomb bloke suggested this the other day:
local f = fs.open("file","r")
local file = {}
for line in f.readLine do
  table.insert(file,line)
end
f.close()

Just a bit shorter, does the same thing

Edit: Actually, jake, yours has a few types, you might want to check yours again
Edit2: Type fix! Removed ()

Edited by HPWebcamAble, 14 April 2015 - 03:28 AM.


#13 jakejakey

  • Members
  • 98 posts

Posted 14 April 2015 - 03:08 AM

ah okay im gonna remove my post then

#14 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 14 April 2015 - 03:22 AM

View PostHPWebcamAble, on 14 April 2015 - 02:07 AM, said:

Edit: Actually, jake, yours has a few types, you might want to check yours again

Did I really write it like that? I hope not, 'cause that won't work... you'd need to use f.readLine there, as opposed to f.readLine().

But it turns out the io API has io.lines(), which handles the file opening / closing for you:

local file = {}
for line in io.lines("file") do table.insert(file,line) end

... but still, I was under the impression that Leon's line only had the one line in it. For a single-line file you might even be able to crush it down to something like:

local myLine = io.lines("file")()


#15 HPWebcamAble

  • Members
  • 933 posts
  • LocationWeb Development

Posted 14 April 2015 - 03:33 AM

View PostBomb Bloke, on 14 April 2015 - 03:22 AM, said:

Did I really write it like that? I hope not, 'cause that won't work... you'd need to use f.readLine there, as opposed to f.readLine().

D: Sorry I wrote it from memory, I fixed that in my post

View PostBomb Bloke, on 14 April 2015 - 03:22 AM, said:

local myLine = io.lines("file")()

Seriously? Haha one day we'll compact programs so much they'll just be a single character

Spoiler

Edited by HPWebcamAble, 14 April 2015 - 03:34 AM.






3 user(s) are reading this topic

0 members, 3 guests, 0 anonymous users