Forums » MacOS X

Help learning Lua

«123»
Jun 08, 2006 Person link
lol ananzi, YOU'RE the one who gets the gold medal for persistance! :D
Thanks for stickin' with me throughout all my Lua n00bishness, man.

I think I've almost... almost...

/me runs to knock on wood...

I'll post tomorrow morning when I make it or brake it. Considering my track record, I'll probably brake something else now.

-Calder

P.S. Ananzi, thanks for the file, but I was at least smart enough to make a backup of it, :D The problem arose because when I reverted to the backup, it didn't go back to the old access privileges. 3 W00TS for Disk Utility!
/me fixed it
Jun 09, 2006 Person link
This is like a bad nightmare. When I go to install the thing, I get 1 error. 1 simple error, but it stops it from working. Terminal says it can't find /bin/sh:

I used Tinkertool to turn on 'show hidden files', navigated to that directory, and lo and behold: it's there! I have know idea what Unix is throwing a fit about now...

-Calder
Jun 09, 2006 ananzi link
whoever packaged lua didnt do it properly. but you dont need the doc directory.

try this:
/usr/local/bin/lua5.1

should give you a lua prompt. ctrl-c to exit.

----

now try this:

lua5.1

should give you a lua prompt. if not, add /usr/local/bin to your PATH (see last page)

-----

then try this:

sudo ln -s /usr/local/bin/lua5.1 /usr/local/bin/lua

lua

(so you dont have to type lua5.1 all the time)
Jun 09, 2006 Person link
Thank you ananzi! I've got everything working now except for the shortcut. I don't have a .bash file in my home folder. Is there any way I could copy yours, and then add the stuff? Or would that not work...

-Calder
Jun 09, 2006 Person link
Ananzi, thank you SO MUCH again!!! After all this time, IT WORKS!!! I have my first program up and running. It's a simple factorial program, but... yeah, it's my first program.

#!/usr/bin/lua

function factorialP (n)
if n == 0 then
return 1
else
return n * factorialP(n-1)
end
end

function factorialN (n)
if n == 0 then
return 1
else
return n * factorialN(n+1)
end
end

print("This program is dedicated to ananzi :D")
print("Please enter a number:")
a = io.read("*number")
if a == 0 then
print("The factorial of 0 is 0")
else
b = a
if a < 0 then
print("The factorial of " .. b .. " is " .. factorialN(a))
else
print("The factorial of " .. b .. " is " .. factorialP(a))
end
end

Thanks again for everything,
-Calder
Jun 09, 2006 ananzi link
whats up with #!/usr/bin/lua? it should be #!/usr/local/bin/lua to use the one you just compiled.

if you already had lua in /usr/bin then this whole thread was unnecessary. you could check the date & stuff on /usr/bin/lua to see when it was installed and by who (ls -l /usr/bin/lua).

Jun 09, 2006 Person link
All I know is that the program works with #!usr/bin/lua.

Wait, so you mean I already HAD Lua installed? How the hell...
/me is so confused...

Seriously, it's more likely that the installer goofed up than it is that this computer came installed with Lua.

-Calder
Jun 09, 2006 ananzi link
well, my /usr/bin/lua is a symbolic link to /usr/local/bin/lua5.1

i have no idea how it got there. the lua installer doesn't put it there.

whatever.

here is my .bash_profile
if you are using csh instead of bash this wont work. run 'ps' to see if u are running bash.

alias nano='nano -T 4'
alias ls='ls -Gh'
export DISPLAY=:0

# . /sw/bin/init.sh # i turn this on if im using fink (you dont need fink for lua)

export PATH=$PATH:/usr/local/bin
Jun 09, 2006 ananzi link
here's some lua code that worked on vendetta for a few days when they opened up the lua console interface.

unfortunately the formatting tag commands in this @#$@(#$) message board are some gigantic f@#$@#$ undocumented secret, because we all know how dangerous the <pre> html tag is, but hopefully you can get the idea.

[monospace]
<code>
[code]
[<pre>]
[pre]
<pre>
[[code]]

------ extra.lua, extra crap for vendetta online -------

--------- quaternion and vector stuff ----------------------

function prquat(q) -- print quaternion
print(quaternion.x(q))
print(quaternion.y(q))
print(quaternion.z(q))
print(quaternion.w(q))
end

function prvec(v) -- print vector
print(gvector.x(v))
print(gvector.y(v))
print(gvector.z(v))
end

----- make a zero vec, for convenience
zerovec = gvector.clone(OEObject.GetPosition(me()))
gvector.SetX(zerovec,0)
gvector.SetY(zerovec,0)
gvector.SetZ(zerovec,0)

----- set vector manually with x,y,z
function setvec(v,x,y,z)
gvector.SetX(v,x)
gvector.SetY(v,y)
gvector.SetZ(v,z)
end

--- a few vectors to play around with
v1=gvector.clone(zerovec)
v2=gvector.clone(zerovec)
v3=gvector.clone(zerovec)

---- zero quat, for convenience
zeroquat = quaternion.clone(OEObject.GetOrientation(me()))
quaternion.zero(zeroquat)

--- set quat manually with x,y,z,w
function setquat(q,x,y,z,w)
quaternion.SetX(q,x)
quaternion.SetY(q,y)
quaternion.SetZ(q,z)
quaternion.SetW(q,w)
end

-- few quats to play around with
q1=quaternion.clone(zeroquat)
q2=quaternion.clone(zeroquat)
q3=quaternion.clone(zeroquat)

--------------- fake explosion stuff ----------------

function smoke() -- yes, it will hurt you, but you wont die
OEObject.PseudoExplode(me())
end

--------------- teleportation stuff --------------------

-- within same sector only

-- vendettaonline uses 'gvector' for vectors

function jump_relative(jx,jy,jz) -- jump relative to current position
print('jumping relative '..jx..','..jy..','..jz)
v = my_position()
x = gvector.x(v)
y = gvector.y(v)
z = gvector.z(v)
newv = gvector.clone(v)
setvec(newv,x+jx,y+jy,z+jz)
OEObject.SetPosition(me(),newv)
end

function jump_absolute(jx,jy,jz) -- absolute coordinate, xyz
newv = gvector.clone(zerovec)
setvec(newv,jx,jy,jz)
jump_absolute_vec(newv)
end

function jump_absolute_vec(nv) -- absolute coordinate, given via a gvector
OEObject.SetPosition(me(),nv)
end

function jump0() -- jump to 0,0,0, usually near a station/roids
OEObject.SetPosition(me(),zerovec)
end

function my_position() -- returns my position as a gvector
return OEObject.GetPosition(me())
end

function my_position_string() -- returns my xyz position as a string, like '0.4 44.3 492.1'
p=OEObject.GetPosition(me())
return gvector.x(p)..','..gvector.y(p)..','..gvector.z(p)
end

-------------- my ship/items stuff ----------------------

function me()
return OE.GetLocalObjects()[1]
end

function my_ship()
return player.activeship
end

function my_cargo()
return client.GetShipInventory(my_ship().item)['cargo']
end

------------------- cargo stuff -------------------------

function dumpone()
-- dump a single piece of cargo
print('dumping at '..my_position_string())
cargoid = my_cargo()[1]:ID()
client.JettisonSingle(cargoid,1)
end

------- shipwrite -- write messages with dumped cargo ------

himessage= [[
. . .
. . .
.... .
. . .
. . .
]]

mlmessage= [[
. . . . . ... . .. . . ....
.. .. . . . . . . . . . . .
. . . . . . ... . . . . . . .
. . . . . .. ... . . . . . ...
. . ..... . . . . . . . . .
. . . . . . . . . . ... .
. . . . . . ... .... .. . ....
]]

nwmessage= [[
. . .. ..... . . . ....
.. . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . ....
. . . . . . . . . ..... . .
. .. . . . . . . . . . . .
. . .. . . . . . . .
]]

--- 'x' is left to right
--- 'y' is up and down

shipwrite_linedist = 35 -- units between lines
shipwrite_dotdist = 35 -- units between pixels/dots within a line

function shipwrite_newline() -- a 'newline', moves your ship down and back
jump_absolute_vec(shipwrite_lastlinestart)
jump_relative(0,shipwrite_linedist,0)
shipwrite_lastlinestart = my_position()
end

function shipwrite_init() -- call this first
shipwrite_lastlinestart = my_position()
end

function shipwrite(c) -- print a single character, and advance
if c=='\n' then shipwrite_newline() end
if c==' ' then end
if c=='.' then dumpone() end
jump_relative(shipwrite_dotdist,0,0)
end

function printmessage(message) -- doesnt work, see kludge below
print('printmessage doesnt work')
shipwrite_init()
for i = 1, string.len(message), 1 do
x=string.byte(message,i)
if x==10 then shipwrite('\n') end
if x==32 then shipwrite(' ') end
if x==46 then shipwrite('.') end
end
print('printmessage doesnt work')
end

function countcargo(message) -- how many cargo u need for message
cargocount=0
for i = 1, string.len(message), 1 do
x=string.byte(message,i)
if x==46 then cargocount = cargocount + 1 end
end
return cargocount
end

---------- kludge because events are screwed up/batched ---------

----- how to use

-- 0. get VO beta 8
-- 1. put extra.lua into the directory where your vendetta beta 8 is
-- 2. get a moth with 120 of aquean ore or whatever
-- 3. launch.
-- 4. hit `, should bring up console
-- 5. type /lua dofile('extra.lua')
-- 6. type /lua setmessage(himessage)
-- 7. type /lua donextdot()
-- 8. now, hit up arrow, then enter, then up arrow, the enter, ad infinitum
-- 9. after a while your ship will have written a message in space!
--10. messages are listed above, written in 'dots', periods and spaces basically.

kludgemessage = ''
dotcounter=1
kludgemessagelength = 0
lastdot=0

function setmessage(message) -- set what message yr gonna write
shipwrite_init()
kludgemessage=message
kludgemessagelength = string.len(kludgemessage)
dotcounter=1
lastdot=string.byte(kludgemessage,1)
print('starting message: '..kludgemessage)
print('length'..kludgemessagelength)
print('cargos'..countcargo(message))
end

function donextdot() -- do the next 'dot' in the message set by setmessage()
-- gotta hit uparrow, enter, uparrow, enter, over and over
if lastdot==46 then shipwrite('.') end
repeat
lastdot=string.byte(kludgemessage,dotcounter)
if lastdot==10 then shipwrite('\n') end
if lastdot==32 then shipwrite(' ') end
dotcounter=dotcounter+1
until lastdot==46 or dotcounter>=kludgemessagelength
if dotcounter>=kludgemessagelength then print('finished') end
end



------------- fake stuff, for testing outside of VO ----------

--[[

function printmessage()
for i = 1, string.len(message), 1 do
x=string.byte(message,i)
if x==10 then io.write('\n') end
if x==32 then io.write(' ') end
if x==46 then io.write('.') end
end
end

function jump_absolute_vec(vec) -- fake, for testing
count=1
for word in string.gfind(vec, "%d+") do
if count==1 then shipx=word end
if count==2 then shipy=word end
if count==3 then shipz=word end
count=count+1
end
shipvec = shipx .. ' ' .. shipy .. ' ' .. shipz
end

function jump_absolute(x,y,z) -- fake, for testing
shipx=x; shipy=y; shipz=z;
shipvec = shipx .. ' ' .. shipy .. ' ' .. shipz
end

function jump_relative(x,y,z) -- fake, for testing
shipx=shipx+x; shipy=shipy+y; shipz=shipz+z;
shipvec = shipx .. ' ' .. shipy .. ' ' .. shipz
end

shipx=0
shipy=0
shipz=0
shipvec="0 0 0"

function my_position() -- fake, for testing
return shipvec
end

function dumpone() -- fake, for testing
print('dump');
end

function showpos() -- fake, for testing
print(shipx..','..shipy..','..shipz); print(shipvec);
end

]]--

[/monospace]
</code>
[/code]
[</pre>]
[/pre]
</pre>
[[/code]]
Jun 09, 2006 Suicidal Lemming link
ananzi, I don't think there is a <pre> type tag for the messageboard, but I'll make a suggestion in the forum suggestion thread.
Jun 16, 2006 Person link
Well, I finally got the PiL second edition. I've written two more programs and I'm currently working on emulating my Enigma program in Lua to make it run faster.



Quadratic:
#!/usr/local/bin/lua5.1

print ("You will enter A, B and C in the equation Ax^2+Bx+C=0:")
print ("A:")
a = io.read ("*number")
print ("B:")
b = io.read ("*number")
print ("C:")
c = io.read ("*number")

disc = b ^ 2 - 4 * a * c

rest1 = (-1 * b + disc ^ .5) / 2 / a
rest2 = (-1 * b - disc ^ .5) / 2 / a

print (rest1 .. " and " .. rest2)




Factors:
#!/usr/local/bin/lua5.1

function factor (nc)
ncf = math.floor(nc)
if ncf == nc
then
factors[tn] = nc
tn = tn + 1
return(True)
else
return(False)
end
end

function final (tct)
if tct == 1
then
return("and " .. numb .. ".")
else
return(factors[tct] .. ", " .. final(tct - 1))
end
end

print("Enter number to find all factors:")
numb = io.read("*number")

factors = {}
current = 1
tn = 1
if math.floor(numb / 2) == (numb / 2)
then
numbdos = numb / 2
else
numbdos = math.floor(numb / 3)
end
for i=1, numbdos
do
factor(numb / current)
current = current + 1
end

print("Factors of " .. numb .. " are: " .. final(#factors))




More to come!
-Calder
Jun 17, 2006 Person link
OMG OMG OMG! It took me until now, (1:30 in the morning) but I finished my lua port of my original enigma program. It's stored in two files, Enigma.txt and Settings.txt. If anyone cares, I'll post them in the morning when I'm not so totally wasted from sleep deprivation.

-Calder
Jun 18, 2006 Person link
Well, I finished my Enigma program. It's working, yeyyeyyey. I'm working on a random code generator, but I ran into some serious problems with the # function. Either it's borked, or I am. So I have this table, ups, and I filled everything up to 100 with the value 'filled'. I have a program that randomly fills in stuff and once it's completed a number it checks it off in ups. Here's the problem, the # function is not working correctly. Here's the values of the table:

ups[101] =
nil
ups[102] =
filled
ups[103] =
filled
ups[104] =
filled
ups[105] =
filled
ups[106] =
filled
ups[107] =
filled
ups[108] =
nil
ups[109] =
nil
ups[110] =
filled
ups[111] =
nil
ups[112] =
filled
ups[113] =
nil
ups[114] =
filled
ups[115] =
filled
ups[116] =
nil
ups[117] =
nil
ups[118] =
filled
ups[119] =
filled
ups[120] =
filled
ups[121] =
filled
ups[122] =
filled
ups[123] =
filled
ups[124] =
filled
ups[125] =
filled
ups[126] =
filled
ups[127] =
filled
ups[128] =
filled
ups[129] =
filled
ups[130] =
filled
ups[131] =
filled
ups[132] =
filled
ups[133] =
filled
ups[134] =
filled
ups[135] =
filled
ups[136] =
filled
ups[137] =
filled
ups[138] =
filled
ups[139] =
filled
ups[140] =
filled
ups[141] =
filled
ups[142] =
filled
ups[143] =
filled
ups[144] =
filled
ups[145] =
filled
ups[146] =
filled
ups[147] =
filled
ups[148] =
filled
ups[149] =
filled
ups[150] =
filled
ups[151] =
filled
ups[152] =
filled
ups[153] =
filled
ups[154] =
filled
ups[155] =
filled
ups[156] =
filled
ups[157] =
filled
ups[158] =
filled
ups[159] =
filled
ups[160] =
filled
ups[161] =
filled
ups[162] =
filled
ups[163] =
filled
ups[164] =
filled
ups[165] =
filled
ups[166] =
filled
ups[167] =
filled
ups[168] =
filled
ups[169] =
filled
ups[170] =
filled
ups[171] =
filled
ups[172] =
filled
ups[173] =
filled
ups[174] =
filled
ups[175] =
filled
ups[176] =
filled
ups[177] =
filled
ups[178] =
filled
ups[179] =
filled
ups[180] =
filled
ups[181] =
filled
ups[182] =
filled
ups[183] =
filled
ups[184] =
filled
ups[185] =
filled
ups[186] =
filled
ups[187] =
filled
ups[188] =
filled
ups[189] =
filled
ups[190] =
filled
ups[191] =
filled
ups[192] =
filled
ups[193] =
filled
ups[194] =
filled
ups[195] =
filled
ups[196] =
filled
ups[197] =
filled
ups[198] =
filled
ups[199] =
filled
ups[200] =
filled
ups[201] =
nil
ups[202] =
nil
ups[203] =
filled
ups[204] =
nil
ups[205] =
filled
ups[206] =
nil
ups[207] =
filled
ups[208] =
filled
ups[209] =
filled
ups[210] =
filled
ups[211] =
nil
ups[212] =
filled
ups[213] =
nil
ups[214] =
filled
ups[215] =
filled
ups[216] =
filled
ups[217] =
filled
ups[218] =
filled
ups[219] =
nil
ups[220] =
nil
ups[221] =
nil
ups[222] =
nil
ups[223] =
nil
ups[224] =
nil
ups[225] =
nil
ups[226] =
nil
ups[227] =
nil
ups[228] =
nil
ups[229] =
nil
ups[230] =
nil
ups[231] =
nil
ups[232] =
nil
ups[233] =
nil
ups[234] =
nil
ups[235] =
nil
ups[236] =
nil
ups[237] =
nil
ups[238] =
nil
ups[239] =
nil
ups[240] =
nil
ups[241] =
nil
ups[242] =
nil
ups[243] =
nil
ups[244] =
nil
ups[245] =
nil
ups[246] =
nil
ups[247] =
nil
ups[248] =
nil
ups[249] =
nil
ups[250] =
nil
ups[251] =
nil
ups[252] =
nil
ups[253] =
nil
ups[254] =
nil
ups[255] =
nil
ups[256] =
nil
ups[257] =
nil
ups[258] =
nil
ups[259] =
nil
ups[260] =
nil
ups[261] =
nil
ups[262] =
nil
ups[263] =
nil
ups[264] =
nil
ups[265] =
nil
ups[266] =
nil
ups[267] =
nil
ups[268] =
nil
ups[269] =
nil
ups[270] =
nil
ups[271] =
nil
ups[272] =
nil
ups[273] =
nil
ups[274] =
nil
ups[275] =
nil
ups[276] =
nil
ups[277] =
nil
ups[278] =
nil
ups[279] =
nil
ups[280] =
nil
ups[281] =
nil
ups[282] =
nil
ups[283] =
nil
ups[284] =
nil
ups[285] =
nil
ups[286] =
nil
ups[287] =
nil
ups[288] =
nil
ups[289] =
nil
ups[290] =
nil
ups[291] =
nil
ups[292] =
nil
ups[293] =
nil
ups[294] =
nil
ups[295] =
nil
ups[296] =
nil
ups[297] =
nil
ups[298] =
nil
ups[299] =
nil
ups[300] =
nil
ups[301] =
nil
ups[302] =
nil
ups[303] =
filled
ups[304] =
nil
ups[305] =
filled
ups[306] =
nil
ups[307] =
filled
ups[308] =
nil
ups[309] =
filled
ups[310] =
nil
ups[311] =
nil
ups[312] =
filled
ups[313] =
nil
ups[314] =
nil
ups[315] =
nil
ups[316] =
filled
ups[317] =
nil
ups[318] =
nil
ups[319] =
filled
ups[320] =
filled
218
letterpos["7"] = 220
ups[101] =
nil
ups[102] =
filled
ups[103] =
filled
ups[104] =
filled
ups[105] =
filled
ups[106] =
filled
ups[107] =
filled
ups[108] =
nil
ups[109] =
nil
ups[110] =
filled
ups[111] =
nil
ups[112] =
filled
ups[113] =
nil
ups[114] =
filled
ups[115] =
filled
ups[116] =
nil
ups[117] =
nil
ups[118] =
filled
ups[119] =
filled
ups[120] =
filled
ups[121] =
filled
ups[122] =
filled
ups[123] =
filled
ups[124] =
filled
ups[125] =
filled
ups[126] =
filled
ups[127] =
filled
ups[128] =
filled
ups[129] =
filled
ups[130] =
filled
ups[131] =
filled
ups[132] =
filled
ups[133] =
filled
ups[134] =
filled
ups[135] =
filled
ups[136] =
filled
ups[137] =
filled
ups[138] =
filled
ups[139] =
filled
ups[140] =
filled
ups[141] =
filled
ups[142] =
filled
ups[143] =
filled
ups[144] =
filled
ups[145] =
filled
ups[146] =
filled
ups[147] =
filled
ups[148] =
filled
ups[149] =
filled
ups[150] =
filled
ups[151] =
filled
ups[152] =
filled
ups[153] =
filled
ups[154] =
filled
ups[155] =
filled
ups[156] =
filled
ups[157] =
filled
ups[158] =
filled
ups[159] =
filled
ups[160] =
filled
ups[161] =
filled
ups[162] =
filled
ups[163] =
filled
ups[164] =
filled
ups[165] =
filled
ups[166] =
filled
ups[167] =
filled
ups[168] =
filled
ups[169] =
filled
ups[170] =
filled
ups[171] =
filled
ups[172] =
filled
ups[173] =
filled
ups[174] =
filled
ups[175] =
filled
ups[176] =
filled
ups[177] =
filled
ups[178] =
filled
ups[179] =
filled
ups[180] =
filled
ups[181] =
filled
ups[182] =
filled
ups[183] =
filled
ups[184] =
filled
ups[185] =
filled
ups[186] =
filled
ups[187] =
filled
ups[188] =
filled
ups[189] =
filled
ups[190] =
filled
ups[191] =
filled
ups[192] =
filled
ups[193] =
filled
ups[194] =
filled
ups[195] =
filled
ups[196] =
filled
ups[197] =
filled
ups[198] =
filled
ups[199] =
filled
ups[200] =
filled
ups[201] =
nil
ups[202] =
nil
ups[203] =
filled
ups[204] =
nil
ups[205] =
filled
ups[206] =
nil
ups[207] =
filled
ups[208] =
filled
ups[209] =
filled
ups[210] =
filled
ups[211] =
nil
ups[212] =
filled
ups[213] =
nil
ups[214] =
filled
ups[215] =
filled
ups[216] =
filled
ups[217] =
filled
ups[218] =
filled
ups[219] =
nil
ups[220] =
filled
ups[221] =
nil
ups[222] =
nil
ups[223] =
nil
ups[224] =
nil
ups[225] =
nil
ups[226] =
nil
ups[227] =
nil
ups[228] =
nil
ups[229] =
nil
ups[230] =
nil
ups[231] =
nil
ups[232] =
nil
ups[233] =
nil
ups[234] =
nil
ups[235] =
nil
ups[236] =
nil
ups[237] =
nil
ups[238] =
nil
ups[239] =
nil
ups[240] =
nil
ups[241] =
nil
ups[242] =
nil
ups[243] =
nil
ups[244] =
nil
ups[245] =
nil
ups[246] =
nil
ups[247] =
nil
ups[248] =
nil
ups[249] =
nil
ups[250] =
nil
ups[251] =
nil
ups[252] =
nil
ups[253] =
nil
ups[254] =
nil
ups[255] =
nil
ups[256] =
nil
ups[257] =
nil
ups[258] =
nil
ups[259] =
nil
ups[260] =
nil
ups[261] =
nil
ups[262] =
nil
ups[263] =
nil
ups[264] =
nil
ups[265] =
nil
ups[266] =
nil
ups[267] =
nil
ups[268] =
nil
ups[269] =
nil
ups[270] =
nil
ups[271] =
nil
ups[272] =
nil
ups[273] =
nil
ups[274] =
nil
ups[275] =
nil
ups[276] =
nil
ups[277] =
nil
ups[278] =
nil
ups[279] =
nil
ups[280] =
nil
ups[281] =
nil
ups[282] =
nil
ups[283] =
nil
ups[284] =
nil
ups[285] =
nil
ups[286] =
nil
ups[287] =
nil
ups[288] =
nil
ups[289] =
nil
ups[290] =
nil
ups[291] =
nil
ups[292] =
nil
ups[293] =
nil
ups[294] =
nil
ups[295] =
nil
ups[296] =
nil
ups[297] =
nil
ups[298] =
nil
ups[299] =
nil
ups[300] =
nil
ups[301] =
nil
ups[302] =
nil
ups[303] =
filled
ups[304] =
nil
ups[305] =
filled
ups[306] =
nil
ups[307] =
filled
ups[308] =
nil
ups[309] =
filled
ups[310] =
nil
ups[311] =
nil
ups[312] =
filled
ups[313] =
nil
ups[314] =
nil
ups[315] =
nil
ups[316] =
filled
ups[317] =
nil
ups[318] =
nil
ups[319] =
filled
ups[320] =
filled


and here's what the #ups command returns: 220

Yeah, any ideas what's borked? Does the # not work with tables? PiL said it did, and it's worked before, but... yeah, I'm clueless.

-Calder
Jun 19, 2006 ananzi link
if you dont post any source code, it will be extremely difficult for anyone to answer your question.
Jun 19, 2006 Person link
here's teh source code:

Random.txt:
#!/usr/local/bin/lua5.1

--functions:
function fill (cl)
temp = ((math.random(3)*100) + math.random(20))
if ups[temp] == nil
then
letterpos[cl] = temp
ups[temp] = "filled"
print("letterpos[\"" .. cl .. "\"] = " .. temp)
--[=[
--I commented this section out, but this is the section that generated the results you saw
derder = 101
for i=101, 320
do
print("ups[" .. derder .. "] = ")
print(ups[derder])
derder = derder + 1
end
print(#ups)
--]=]
return(string.sub(assignl, 2, #assignl))
else
return(assignl)
end
end

--initiation
ups = {}
letterpos = {}
math.randomseed(os.time())

--fill table gaps
counter = 1
for i=1, 100
do
ups[counter] = "filled"
counter = counter + 1
end

--add random positions
assignl = ("abcdefghijklmnopqrstuvwxyz0123456789,'$-\":[!.@^*#] ?;&+=)%/")
repeat
assignl = (fill(string.sub(assignl, 1, 1)))
until #ups == 120

counter = 121
for i=121, 200
do
ups[counter] = "filled"
counter = counter + 1
end
repeat
assignl = (fill(string.sub(assignl, 1, 1)))
until #ups == 220
print(#ups)
counter = 221
for i=221, 300
do
ups[counter] = "filled"
counter = counter + 1
end
repeat
assignl = (fill(string.sub(assignl, 1, 1)))
until #ups == 320
print(#ups)


-Calder
Jun 20, 2006 ananzi link
One possible answer lies within the lua reference manual. This section contains the definition of the '#' operator.

http://www.lua.org/manual/5.1/manual.html#2.5.5
Jun 21, 2006 Person link
Oh damn. So it's almost random which one of my false 'ends' the # function picks. Is there ANY command to have it determine the table length by the first 'nil' value, or should I write my own command to do that?

-Calder
Jun 21, 2006 ananzi link
i have no idea. if i were you, i'd google 'table length lua'.
then take the stuff you find there (like getn) and google it too.
Jun 23, 2006 Person link
I got it working by writing my own command for table length:

function tablelength(table)
length = 1
done = 0
repeat
if table[length] == nil
then
length = length - 1
done = 1
else
length = length + 1
end
until done == 1
return(length)
end


Does anyone want to try out my über-fast Lua port of my old Enigma program? I'll put the source code here if you want it...

Ok, this is really random but I got thinking the other day, (VERY rare occurence, BTW) and I was wondering:
does Vendetta install Lua on all our computers, or is the Lua code in Vendetta compiled into C or something? If so, how do you do that?

-Calder
Jun 24, 2006 ananzi link
i am guessing its called 'embedding'. i am assuming that the bulk of vendetta is written in C/C++, and uses lua as an 'embedded' language.

google 'embedding lua':

http://heavycoder.com/tutorials/lua_embed.php

a comparison between embedding python and lua, for games, no less. plus some people talking about 'extending' instead of embedding.

http://lua-users.org/wiki/LuaVersusPython