Thread Tools Display Modes
12-28-09, 01:22 PM   #1
d20hero
A Murloc Raider
Join Date: Dec 2009
Posts: 9
Converting string to array in LUA! please help :(

I'm wondering if there is a way to treat a string in lua like an array to access each element of the string character by character and how to initialize the array to do so. My limited understanding is that a table is essentially an array but I can't find the string library to see how lua treats the member functions so I can handle it myself. Trying to return a value such as Name(unit) back to a variable to hold the string then access each character so that I can use string.len() to see if its over the amount and then use a loop to only display so many characters in that string. Is there even a function made for this already? TY to anyone who can help.
  Reply With Quote
12-28-09, 01:35 PM   #2
Akryn
A Firelord
AddOn Author - Click to view addons
Join Date: Mar 2008
Posts: 479
newString = string:sub(1, n)

http://www.lua.org/pil/index.html#20
  Reply With Quote
12-28-09, 01:36 PM   #3
Xus
A Fallenroot Satyr
 
Xus's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2007
Posts: 25
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.
http://www.lua.org/manual/5.1/ is your friend
  Reply With Quote
12-28-09, 02:03 PM   #4
d20hero
A Murloc Raider
Join Date: Dec 2009
Posts: 9
Cool but how to i initialize the array with a function call? Or are you suggesting I don't and just use that to split the string open?

Last edited by d20hero : 12-28-09 at 02:05 PM.
  Reply With Quote
12-28-09, 02:14 PM   #5
d20hero
A Murloc Raider
Join Date: Dec 2009
Posts: 9
Nvm i figured it out. I don't like the notation they show to use it but the sub string function is so freaking powerful and easy to use woot ty guys.

local namespace = Name(unit)
if string.len(namespace) > 25 then
return '%s... ', string.sub (namespace, 1 , 24)
else

Last edited by d20hero : 12-28-09 at 02:34 PM.
  Reply With Quote
12-28-09, 03:33 PM   #6
haste
Featured Artist
 
haste's Avatar
Premium Member
Featured
Join Date: Dec 2005
Posts: 1,027
You might want to read some in this thread as well. string.sub() isn't multi-byte aware.
__________________
「貴方は1人じゃないよ」
  Reply With Quote
12-28-09, 03:54 PM   #7
acapela
A Cobalt Mageweaver
AddOn Author - Click to view addons
Join Date: Feb 2007
Posts: 241
Originally Posted by haste View Post
You might want to read some in this thread as well. string.sub() isn't multi-byte aware.
yeah, for locale portability, you have to be careful with this.

FYI, i found some code in the implementation of LibDogTags-3.0's "Truncate" tag, that walks multi-byte strings and slices them up into multi-byte characters (in the form of substrings). this is now part of Aloft's "Truncate" tag implementation (courtesy of ckknight, shefki, etc).

there are probably other things (search the web?) that can provide examples.
__________________
Retired author/maintainer of Aloft (the nameplate addon)
http://www.wowinterface.com/download...AloftBeta.html
-----
Zippy said it best: "All life is a BLUR of Republicans and Meat!"

Last edited by acapela : 12-28-09 at 03:57 PM.
  Reply With Quote

WoWInterface » AddOns, Compilations, Macros » AddOn Help/Support » Converting string to array in LUA! please help :(


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off