[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: string[], doesn't exist?
- From: Sam Roberts <sroberts@...>
- Date: Wed, 21 Jun 2006 10:50:56 -0700
On Wed, Jun 21, 2006 at 01:37:01PM -0300, Luiz Henrique de Figueiredo wrote:
> > why does [] not apply to strings?
>
> It does, but it does not do what you want. But try this:
>
> s="lua"
> print(s[2])
> getmetatable("").__index = function (s,i)
> if type(i)=="number" then
> return string.sub(s,i,i)
> else
> return string[i]
> end
> end
> print(s[2])
This is pretty useful, so I was thinking [] could be used for bit access
on numbers, too, but:
n=42
getmetatable(0).__index = function (s,i)
return 13 -- random value to see if this is possible
end
print(n[2])
Doesn't work... I guess strings have metatables, but numbers don't?
Whats going on here?
Sam
- References:
- Bitwsie operators in Lua, mike krimerman
- Re: Bitwsie operators in Lua, Gavin Wraith
- Re: Bitwsie operators in Lua, Sam Roberts
- Re: Bitwsie operators in Lua, Andy Stark
- Re: Bitwsie operators in Lua, Doug Rogers
- Re: Bitwsie operators in Lua, Andy Stark
- Re: Bitwsie operators in Lua, Roberto Ierusalimschy
- string[], doesn't exist?, Sam Roberts
- Re: string[], doesn't exist?, Luiz Henrique de Figueiredo