Reputation: 3697
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.
Upvotes: 1
Views: 1969
Reputation: 72402
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
Upvotes: 1
Reputation: 122493
Unlike some other languages, Lua uses ..
to concatenate strings, not +
, change
s = s + '-'
to
s = s .. '-'
Upvotes: 4