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

string.sub question

Started by Varscott11, 26 June 2016 - 02:58 PM
Varscott11 #1
Posted 26 June 2016 - 04:58 PM
So I have a program that needs to take a string received through rednet, and determine what the last few letters are.
I have set up a string.sub to handle this that looks like this:

id, message = rednet.receive()
messagesub = string.sub(message, pos1, pos2)

I've tried:

local messagelength = #message - 4 –subtract for from the total length
message = string.sub(message, messagelength)

However, its keeps giving me an error, "attempt to get length of nil"

what I was wondering is if there is a way to get the last 4 letters of the string. Any help is appreciated!
TYKUHN2 #2
Posted 26 June 2016 - 05:06 PM
string.sub(message, -4) --#Last 4 characters in the string
Edited on 26 June 2016 - 03:06 PM
Varscott11 #3
Posted 26 June 2016 - 05:09 PM
Thanks for the reply! trying it now

Why thank you good sir. It works!
Exerro #4
Posted 26 June 2016 - 05:39 PM
Or even…


message:sub( -4 )

Your "attempt to get length of nil" error would suggest that 'message' was nil, however, so when you tried "#message", it errored.