Jump to content




Lua question

lua

13 replies to this topic

#1 grand_mind1

  • Members
  • 207 posts

Posted 21 January 2013 - 01:41 PM

If I have something like
check = fs.exists("test")
am I able to run check or do I have to do this with a function? I was just wondering because a 1 line function just seems odd to me.

#2 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 21 January 2013 - 03:18 PM

Not sure what you're asking. That line would see if the file named test exists and store the result (boolean true or boolean false) in the variable named check. Is that what you're trying to do?

#3 SuicidalSTDz

    Permutator of Strings

  • Members
  • 1,308 posts
  • LocationPennsylvania

Posted 21 January 2013 - 03:22 PM

View PostLyqyd, on 21 January 2013 - 03:18 PM, said:

Not sure what you're asking. That line would see if the file named test exists and store the result (boolean true or boolean false) in the variable named check. Is that what you're trying to do?

He is most likely using it in this context:

if check == true then
--do something with the "test" file
else
--create the "test" file
end

EDIT: Would be nice to see the code for better understanding

#4 Axolotl

  • New Members
  • 21 posts

Posted 21 January 2013 - 04:06 PM

You only need to make a function for this if your code is going to use it multiple times otherwise you just leave it like that and use the "check" variable in whatever you're doing.

#5 grand_mind1

  • Members
  • 207 posts

Posted 21 January 2013 - 06:33 PM

Sorry. I mean like could I do this:
check = fs.exists("disk")
while check == false do
    print("Insert Disk")
    sleep(1)
    check --would I be able to do this?
end
Or would I have to make 'check' a function and call it. I would want to use this for checking if a disk is in a disk drive.

#6 brett122798

  • Members
  • 300 posts
  • LocationIn the TARDIS at an unknown place in time.

Posted 21 January 2013 - 06:44 PM

If I'm reading what you're saying correctly, you want this?

function checkDisk()
if fs.exists("disk") then
return true
else
return false
end
end

check = false

while check == false do
	print("Insert Disk")
	sleep(1)
    check = checkDisk()
end


#7 grand_mind1

  • Members
  • 207 posts

Posted 21 January 2013 - 06:46 PM

Yes, you have answered my question. Thanks! Sorry if I was confusing.

#8 brett122798

  • Members
  • 300 posts
  • LocationIn the TARDIS at an unknown place in time.

Posted 21 January 2013 - 06:47 PM

View Postgrand_mind1, on 21 January 2013 - 06:46 PM, said:

Yes, you have answered my question. Thanks! Sorry if I was confusing.
Not a problem. :P

#9 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 21 January 2013 - 06:49 PM

View Postbrett122798, on 21 January 2013 - 06:44 PM, said:

If I'm reading what you're saying correctly, you want this?

function checkDisk()
if fs.exists("disk") then
return true
else
return false
end
end

check = false

while check == false do
	print("Insert Disk")
	sleep(1)
	check = checkDisk()
end

Whoa, seriously? Why not just this:

while not fs.exists("disk") do
  print("Insert Disk")
  sleep(1)
end


#10 grand_mind1

  • Members
  • 207 posts

Posted 21 January 2013 - 06:52 PM

View PostLyqyd, on 21 January 2013 - 06:49 PM, said:

View Postbrett122798, on 21 January 2013 - 06:44 PM, said:

If I'm reading what you're saying correctly, you want this?

function checkDisk()
if fs.exists("disk") then
return true
else
return false
end
end

check = false

while check == false do
	print("Insert Disk")
	sleep(1)
	check = checkDisk()
end

Whoa, seriously? Why not just this:

while not fs.exists("disk") do
  print("Insert Disk")
  sleep(1)
end
I thought I had to do it the more complicated way because you never check for fs.exists("disk") in the loop so it wall always be false even if you put in a disk.

#11 brett122798

  • Members
  • 300 posts
  • LocationIn the TARDIS at an unknown place in time.

Posted 21 January 2013 - 07:01 PM

View PostLyqyd, on 21 January 2013 - 06:49 PM, said:

View Postbrett122798, on 21 January 2013 - 06:44 PM, said:

If I'm reading what you're saying correctly, you want this?

function checkDisk()
if fs.exists("disk") then
return true
else
return false
end
end

check = false

while check == false do
	print("Insert Disk")
	sleep(1)
	check = checkDisk()
end

Whoa, seriously? Why not just this:

while not fs.exists("disk") do
  print("Insert Disk")
  sleep(1)
end
I do currently program inefficiently, but I'm usually just happy it gets the job done..

#12 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 21 January 2013 - 07:05 PM

View Postgrand_mind1, on 21 January 2013 - 06:52 PM, said:

I thought I had to do it the more complicated way because you never check for fs.exists("disk") in the loop so it wall always be false even if you put in a disk.

While loops check the conditional before the loop begins each time it loops, so whenever "disk" came into existence, the conditional would change and the loop would exit at that time.

#13 grand_mind1

  • Members
  • 207 posts

Posted 21 January 2013 - 07:12 PM

View PostLyqyd, on 21 January 2013 - 07:05 PM, said:

View Postgrand_mind1, on 21 January 2013 - 06:52 PM, said:

I thought I had to do it the more complicated way because you never check for fs.exists("disk") in the loop so it wall always be false even if you put in a disk.

While loops check the conditional before the loop begins each time it loops, so whenever "disk" came into existence, the conditional would change and the loop would exit at that time.
Oh, ok! I guess I was just confused on how loops work. Thanks for your help! :D

#14 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 21 January 2013 - 07:16 PM

View Postgrand_mind1, on 21 January 2013 - 07:12 PM, said:

Oh, ok! I guess I was just confused on how loops work. Thanks for your help! :D/>

As stated the while loop checks the conditional before running the instructions. A for loop does the same. It checks the conditional before executing the instructions.
However a repeat loop is different. A repeat loop will always performs the instructions at least once before checking the conditional. Repeat loop syntax:
repeat
  -- code to repeat
until <some condition>






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users