lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


You are unintentionally doubling a every time - instead of appending your original, you append the modified string to itself. so you are doing
a = 'a'
a = a + a = 'a' + 'a' -> a is now 'aa'
a = a + a = 'aa' + 'aa' -> a is now 'aaaa'
a = a + a = 'aaaa' + 'aaaa' -> a is now 'aaaaaaaa'
etc.

Kaj



On Thu, Apr 18, 2013 at 8:19 AM, Chodera, Ian <Ian.Chodera@delphi.com> wrote:

a="k"

n=3

b=""

for c = 1, n do b = b..a end

print(b)

 

From: lua-l-bounces@lists.lua.org [mailto:lua-l-bounces@lists.lua.org] On Behalf Of Chris Datfung
Sent: 18 April 2013 13:16
To: Lua mailing list
Subject: pad a string

 

Hi,

I want to pad a string with itself n number of times, for example if n = 3 and var="test" then var = var .. var .. var

I tried with a for loop but get one too many combinations

> a="k"
> n=3
> for c = 1, n-1 do a = a..a end
> print(a)
kkkk

Is there a smarter way to implement this?

Thanks,

Chris

**************************************************************************************** Note: If the reader of this message is not the intended recipient, or an employee or agent responsible for delivering this message to the intended recipient, you are hereby notified that any dissemination, distribution or copying of this communication is strictly prohibited. If you have received this communication in error, please notify us immediately by replying to the message and deleting it from your computer. Thank you. ****************************************************************************************