lua-users home
lua-l archive

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


s=0; k=0
grade="Ann 57, Bob 64,Cat 77, Don 62, Eve 51"
for n in grade:gmatch("%d+") do s=s+tonumber(n); k=k+1 end
print(s/k)
It's the same, it is easy, and it is clearer for people to understand your code. -> + 10.000 points

> Date: Sat, 7 Jul 2012 13:44:21 +0200
> From: dirk.laurie@gmail.com
> To: lua-l@lists.lua.org
> Subject: Re: Arithmetic on strings
>
> 2012/7/7 steve donovan <steve.j.donovan@gmail.com>:
> > On Sat, Jul 7, 2012 at 11:33 AM, Miles Bader <miles@gnu.org> wrote:
> >> Me too.
> >
> > Ditto. The code it breaks is code that's going to break 'by accident'
> > in some way later.
> >
>
> There's just one thing: input of numbers. At present, I can write:
>
> s=0; k=0
> grade="Ann 57, Bob 64,Cat 77, Don 62, Eve 51"
> for n in grade:gmatch("%d+") do s=s+n; k=k+1 end
> print(s/k)
>
> I don't see this code breaking "by accident".
>
> Are we really going to improve quality-of-life for the public
> by fascistically taking away this freedom?
>
> I vote for this solution: remove string coercion, but make it
> easy to enable/disable the necessary metamethods that give
> the same effect. For at least one release, let string coercion
> by metamethods still be the default.
>