Indexing strings in Lua?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
crow
Party member
Posts: 186
Joined: Thu Feb 24, 2011 11:47 pm
Location: UK
Contact:

Re: Indexing strings in Lua?

Post by crow »

Hey I can see what the rest is doing but what is this one doing ?

Code: Select all

function smt.__index(str, key)
  if type(key) == 'number' then
    return string.sub(str, key, key)
  elseif stringMethods[key] then
    return stringMethods[key]
  end
end
Sir Kittenface
Möko IDE Codename (Erös) Returns Soon

I am dyslexic so if any of my replys confusing please just ask me to reword it as this will make things a lot easier for all parties lol.
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: Indexing strings in Lua?

Post by vrld »

BlackBulletIV wrote:You might want to just make the __index function look at the string library, so can call all string functions from a string itself.
That's already possible...

Code: Select all

local str = 'foo bar baz'
for i in str:match("%w+") do -- same as for i in string.match(str, "%w+")
    print(i, i:sub(1,1))
end

print(("pi = %.5f"):format(math.pi)) -- shorthand for string.format("pi = %.5f", math.pi)
If you really want str:print(), you can do it like so (no need to get the metatable):

Code: Select all

function string.print(str, ...)
    print(str, ...)
end
You can even make shortcuts to often used functions (although it can make your code way harder to read):

Code: Select all

string._ =  string.format
print( ("pi = %.f5"):_(math.pi) )
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: Indexing strings in Lua?

Post by BlackBulletIV »

crow wrote:Hey I can see what the rest is doing but what is this one doing ?

Code: Select all

function smt.__index(str, key)
  if type(key) == 'number' then
    return string.sub(str, key, key)
  elseif stringMethods[key] then
    return stringMethods[key]
  end
end
If the key is a number, it'll get that character. If the key is a member of the stringMethods table, it'll return that.

As for your post vrld, I didn't know that the full string library was indexed. I thought I remember getting an error when I tried one of the functions, but, I obviously didn't.

As for ('f'):print(), I couldn't be stuffed with that, it's actually more characters and I would assume it performs worse.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Indexing strings in Lua?

Post by Robin »

BlackBulletIV wrote:As for ('f'):print(), I couldn't be stuffed with that, it's actually more characters and I would assume it performs worse.
What could be nice:

Code: Select all

function string:print()
    print(self)
    return self
end

do(something(with(strings:print()):print()):print()):print()
Might be useful for debugging.
Help us help you: attach a .love.
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: Indexing strings in Lua?

Post by BlackBulletIV »

Hey that's cool, largely for debugging.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 60 guests