Jump to content




How to avoid blank input?

help lua

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

#1 Bye.

  • Members
  • 59 posts

Posted 21 February 2016 - 02:30 PM

Hi everyone,
I need your help...

I have this code

data = read()
if data == "" then
error
elseif fs.exists(data) then
stuff
else
error
end

How can I check if data is composed by a lot of spaces?
If data is " " then fs,exists() will not give any error messages and the code will crash below

Thanks you!

#2 SquidDev

    Frickin' laser beams | Resident Necromancer

  • Members
  • 1,427 posts
  • LocationDoes anyone put something serious here?

Posted 21 February 2016 - 02:45 PM

You might want to look at Lua patterns. %s can be used to represent any whitespace character and conversely %S can be used to represent any non-whitespace character. You can then use string.find to see if there is a non-whitespace character:

if data:find("%S") then
  print(data)
else
  error("Just whitespace")
end

Edited by SquidDev, 21 February 2016 - 02:46 PM.


#3 Sewbacca

  • Members
  • 450 posts
  • LocationStar Wars

Posted 21 February 2016 - 02:47 PM

With fs.getSize(string path), can you check the Size of the file.

#4 Bye.

  • Members
  • 59 posts

Posted 21 February 2016 - 02:49 PM

View PostSquidDev, on 21 February 2016 - 02:45 PM, said:

You might want to look at Lua patterns. %s can be used to represent any whitespace character and conversely %S can be used to represent any non-whitespace character. You can then use string.find to see if there is a non-whitespace character:

if data:find("%S") then
  print(data)
else
  error("Just whitespace")
end

Thanks you a lot!

#5 Sewbacca

  • Members
  • 450 posts
  • LocationStar Wars

Posted 21 February 2016 - 03:00 PM

View PostSquidDev, on 21 February 2016 - 02:45 PM, said:

You might want to look at Lua patterns. %s can be used to represent any whitespace character and conversely %S can be used to represent any non-whitespace character. You can then use string.find to see if there is a non-whitespace character:

if data:find("%S") then
  print(data)
else
  error("Just whitespace")
end

I think "not find".
if not data:find("%S") then
  print(data)
else
  error("Just whitespace")
end


#6 KingofGamesYami

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

Posted 21 February 2016 - 04:05 PM

View PostSewbacca, on 21 February 2016 - 03:00 PM, said:

if not data:find("%S") then
  print(data)
else
  error("Just whitespace")
end

I think you're confusing %S with %s.

#7 Sewbacca

  • Members
  • 450 posts
  • LocationStar Wars

Posted 21 February 2016 - 05:49 PM

View PostKingofGamesYami, on 21 February 2016 - 04:05 PM, said:

I think you're confusing %S with %s.
Oh :blink:

Edited by Sewbacca, 21 February 2016 - 05:50 PM.






3 user(s) are reading this topic

0 members, 3 guests, 0 anonymous users