|
| 10 Aug 2015 12:17 AM |
| Hello, I am starting to use string manipulations but I am having a bit of trouble figuring this one out. Let's say if I had a long string and I want to pars it so that it will only print 47 characters at a time? And I also want to not pars any direct words in the process and still be under 47 characters? Any help? |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 10 Aug 2015 12:19 AM |
| So print a maximum of 47 characters at a time, but do not interrupt words and instead save them for the next iteration? |
|
|
| Report Abuse |
|
|
|
| 10 Aug 2015 12:21 AM |
| Yep, exactly. I know it has something to do with patterns but like I said, I have just started working on string manipulations and know only the basics of it, any suggestions? |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 10 Aug 2015 12:30 AM |
local longStr = [[a very long, very very very long, paragraph or something. at this point i am just writing stuff to make this fairly long and i think i am doing an awful job at it but at least it is more than fifty characters, i would hope...]]
local idx = 0; local phrases = {}; while idx <= #longStr do local buff = {}; local limit = idx + 47;
while idx <= limit do local phrase = longStr:sub(idx):match("%S+"); if not phrase then break; end if idx + #phrase <= limit then buff[#buff + 1] = phrase; idx = idx + #phrase + 1; else break; end end phrases[#phrases + 1] = table.concat(buff, " "); end
for key = 1, #phrases do print(phrases[key]); end
Not tested, inb4error |
|
|
| Report Abuse |
|
|
|
| 10 Aug 2015 01:11 AM |
local longStr = "Thank you so much cntkillme! I wouldn't have been able to do this by myself at all... And this works perfectly! No errors at all, smooth text. Thank you agian and here is the completed output."
local idx = 0; local phrases = {}; while idx <= #longStr do local buff = {}; local limit = idx + 47;
while idx <= limit do local phrase = longStr:sub(idx):match("%S+"); if not phrase then break; end if idx + #phrase <= limit then buff[#buff + 1] = phrase; idx = idx + #phrase + 1; else break; end end phrases[#phrases + 1] = table.concat(buff, " "); end
for key = 1, #phrases do print(phrases[key]); end
Thank you so much cntkillme! I wouldn't have been able to do this by myself at all... And this works perfectly! No errors at all, smooth text. Thank you agian and here is the completed output. |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 10 Aug 2015 01:13 AM |
I'm so surprised it worked considering i wrote it on this same post, hooray! Also I didn't do a safeguard to check for words that are > 47 chars (why would you do that, but oh well) so you MIGHT run into an infinite loop. |
|
|
| Report Abuse |
|
|
|
| 10 Aug 2015 01:46 AM |
| Well, out of bordome I have been creating a role playing game on my phone with standard Lua (TouchLua). I hit into the issue of my printed strings just getting to long in one line so I found out how many characters can be on one line on my phone and it is 47 characters. I knew there was a faster way to make a function that would split it up for me rather than constantly keeping track of my print lengths and making several different prints at a time for just one string. I just didn't have the slightest clue how I would do it though. This will make my job MUCH easier. Thank you so much! |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 10 Aug 2015 01:48 AM |
| In that case, it actually might be better to overwrite the print function so it does multi-line for you by using the thing I gave you, so you don't have to manually call it AND print each time. |
|
|
| Report Abuse |
|
|
|
| 10 Aug 2015 01:50 AM |
| I just put in a module and made a function for that module called p. This means it is even faster and more efficient then calling the standard print() function. |
|
|
| Report Abuse |
|
|
|
| 10 Aug 2015 02:36 AM |
Cody has outputted this. Love and embrace it.
local subtable = function(tab, s, e) local new = {} local index = 0 for i=s, e do index = index + 1 new[index] = tab[i] end return new end
local indice = function(a, ind) return function(a, i) i = i + (ind or 1) if(a[i])then return i, ind and subtable(a, 1+(i-ind), i) or a[i] end end, a, 0 end
local test = {1,2,3,4,5,6,7,8,9} --Insert ur stuff
for i, v in indice(test, 47) do -- change 47 around print(i, " ---- ", unpack(v)) end
|
|
|
| Report Abuse |
|
|