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.
lesslucid
Prole
Posts: 20
Joined: Sun Mar 20, 2011 8:25 am

Indexing strings in Lua?

Post by lesslucid »

Hallo,

I'm just trying to work my way through some tutorials and there's something that I find quite confusing. I thought I'd make my own simpler version by using the indices of a string to create a table and write a function so that, eg ->

tablifyString("word") -- would produce the output { "w", "o", "r", "d" }

...but unlike in, say, python, where "word"[1] = "o", there doesn't seem to be a way in lua to grab a character out of a string according to its index.
Uh, unless there is, and I just couldn't find it?

Thanks v. much for any help or advice!
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 »

I think it should be like so

Code: Select all

MyStringTable = tablifyString("word");
then to call your letter

Code: Select all

-- w
MyStringTable[1];
-- o
MyStringTable[2];
-- r
MyStringTable[3];
-- d
MyStringTable[4];

OldString = "";
for x =1, 4 do
OldString = OldString..MyStringTable[x];
end
love.graphics.print(OldString, 400, 300);

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.
lesslucid
Prole
Posts: 20
Joined: Sun Mar 20, 2011 8:25 am

Re: Indexing strings in Lua?

Post by lesslucid »

Thanks for your advice!

...I'm still a little lost, though. :(

If I had some way to index characters in a string, I could write a tablifyString function, like this:

Code: Select all

function tablifyString(str)
  local tempTable = {}
  for i, char in ipairs(str)
    temptable[i] = str[i]
  end
  return tempTable
end
Or something like that. But I can't, because for lua then str doesn't mean anything.
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 »

You can either use string.sub for indexing or string.gmatch to convert a string to a table of strings:

Code: Select all

str = "Hello, world"
print(str:sub(1,1), str:sub(2,2)) --> H    e
print(string.sub(str, 3,3)) --> l

function tablify(str)
    local tbl = {}
    for char in str:gmatch(".") do -- matches every character
        tbl[#tbl+1] = char
    end
    return tbl
end
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
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 »

Maybe something like this?

Code: Select all

function tablifyString(str)
  count = string.len(str);
  local tempTable = {};
  for i = 1, string.len(s) do
    temptable[i] = string.sub(str, i, i);
  end
  return tempTable;
end
Edit:
  • Seems vrld got there before me lol
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.
lesslucid
Prole
Posts: 20
Joined: Sun Mar 20, 2011 8:25 am

Re: Indexing strings in Lua?

Post by lesslucid »

Ah, wonderful, thankyou both!
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: Indexing strings in Lua?

Post by Taehl »

lesslucid wrote:...but unlike in, say, python, where "word"[1] = "o", there doesn't seem to be a way in lua to grab a character out of a string according to its index.
Uh, unless there is, and I just couldn't find it?
The Lua manual wrote: string.sub (s, i [, j])
Returns the substring of s that starts at i and continues until j; i and j can be negative. If j is absent, then it is assumed to be equal to -1 (which is the same as the string length). In particular, the call string.sub(s,1,j) returns a prefix of s with length j, and string.sub(s, -i) returns a suffix of s with length i.
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
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 »

You could do something complex like this:

Code: Select all

local smt = getmetatable("")
function smt.__index(str, key)
  if type(key) == 'number' then
    return string.sub(str, key, key)
  end
end
What's happening is I'm getting the metatable for strings (yes, there is one), and creating an __index function to handle the indexing of a non-existent key (such as a number). We can then turn this into a string.sub call, getting only one character. Example:

Code: Select all

('goo')[1] -- 'g'
Note that you can't do this:

Code: Select all

'goo'[1]
Lua doesn't support that syntax.
lesslucid
Prole
Posts: 20
Joined: Sun Mar 20, 2011 8:25 am

Re: Indexing strings in Lua?

Post by lesslucid »

Wow, I'm impressed that that's possible... thanks!
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 »

Yeah, I was pretty excited when I first discovered it. Here's a cool thing you can do:

Code: Select all

local smt = getmetatable("")
local stringMethods = {}

function stringMethods:print()
  print(self)
end

function stringMethods:sub(i, j)
  return string.sub(self, i, j)
end

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
This would allow you to define methods callable on strings via this syntax:

Code: Select all

s = 'hello'
s:print() -- prints 'hello'
You might want to just make the __index function look at the string library, so can call all string functions from a string itself.
Post Reply

Who is online

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