1

I have this small snippet of code here;

for i=1,1000 do
    n=math.floor(math.sin(i/10.0)*40)
    s=''
    for j=1,n do s=s+'-' end
    print(s)
end

But it gives me an error on line 2: "attempt to perform arithmetic on global 's' (a string value)" I don't know why it's doing this, and it's driving me mad.

1
  • 1
    Use string.rep instead of the inner for. Commented Sep 26, 2014 at 12:57

2 Answers 2

4

Unlike some other languages, Lua uses .. to concatenate strings, not +, change

s = s + '-' 

to

s = s .. '-' 
Sign up to request clarification or add additional context in comments.

Comments

1

A loop of string concatenations is not recommended because it leads to a quadratic copy (not that it matters for small strings). Try string.rep instead.

for i=1,1000 do
    n=math.floor(math.sin(i/10.0)*40)
    print(string.rep('-',n))
end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.