Adrian Perez

lua-users home
wiki

Lua user and programmer. Lover of the "small things are beautiful" philosophy.

Current Stuff

Very Old Stuff

        i = 1;
        for line in io.readlines() do
           print(i .. ": ", line);
           i = i+1;
        end

You could do this in one line in C :o) e.g.,
for (int i=1, b[100];;i++) printf("%d: %s\n",i,gets((char*)b));
Or one line in Lua: --NDT
while 1 do i=(i or 0)+1 print(""..i..": "..io.read()) end
  or
for i,ln in function(s,i) return i+1,""..i..": "..io.read() end,nil,1 do print(ln) end

Yeah, you'll right ;), but the C code will crash if you use the program over a file with lines longer than the size of the buffer, and you need to declare the main() function and include stdio.h... Maybe I often worry too much about code correctness and maybe that script was not a good example after all :P -- Adrian

Well.. I don't think any of the last examples given may pass as one line of code. Going like that you can collapse a whole file onto i single line.. Don't get me wrong, I still find it very neat and I like Lua alot :o)

Of course, if terseness is your goal, Perl will win every time: -- ET
while(<>) {print(++$i.": ".$_)}

Heavens! I can't stand Perl, nearly anything I've seen written in Perl seems to me like a bunch of impossible-to-understand-mixed-style-code. But I recognize that your example is unbeatable ;) -- AdrianPerez

Sorry by my interference, but for this problem awk is near invincible (reading from stdin):
awk '{print NR, $0}' -
-- JulioFernandez

Ideas for Stuff

Ideas, ideas: ideas I had but they didn't work, or didn't implement them, or... as I want to work on Lixoo I don't have spare time to these.

I had a go at this one: [moonmake] (documentation: [moonmake.txt]) -- xlq

RecentChanges · preferences
edit · history
Last edited January 25, 2016 9:31 am GMT (diff)