PtokaX forum

Archive => Archived 4.0 boards => Request for Lua 4 scripts => Topic started by: pHaTTy on 25 December, 2003, 20:26:57

Title: Lacking...
Post by: pHaTTy on 25 December, 2003, 20:26:57
Sorry i need to request this one :/

im lacking in knowledge, im spending to much time away from lua, i wrote a script just b4 went to run in ptokax realised id wrote it in a few differ languages :/

well anyways

i want to randomise a some lines in a file

eg

line1
line2
line3
line4

i very lacking here :/
Title:
Post by: plop on 25 December, 2003, 22:40:09
easyest is 2 1st stuff the file into an associative array (table), by numbering the keys.
table = {["1"]=line1, etc....
for example you get 15 keys.
then you do tmp = random(15)
now you can call vallue's (lines) from the table by using table[tmp].

plop
Title:
Post by: pHaTTy on 25 December, 2003, 22:42:53
QuoteOriginally posted by plop
easyest is 2 1st stuff the file into an associative array (table), by numbering the keys.
table = {["1"]=line1, etc....
for example you get 15 keys.
then you do tmp = random(15)
now you can call vallue's (lines) from the table by using table[tmp].

plop

hmm not exactly what im after tho :))))

just simple read stright from file and grab each line one by one, then randomize the outcome
Title:
Post by: plop on 25 December, 2003, 22:48:40
you have this file

line1
line2
line3
line4

and want 2 make it like this?

line1
line4
line3
line2

or do you wanne pick a random like from the file?

plop
Title:
Post by: pHaTTy on 25 December, 2003, 23:10:37
yep grab a random line, but directly from the file
Title:
Post by: plop on 25 December, 2003, 23:44:20
never done it like that, i rather store stuff in memory as that is much faster.
but the next skips a random number of lines with a max of 15.
have the idea that this might skip the 1st line but it's up 2 you 2 solve that (easy do).
for i=1,random(15) do
   read("*l")
end
line = read()

plop
Title:
Post by: pHaTTy on 26 December, 2003, 00:01:22
QuoteOriginally posted by plop
never done it like that, i rather store stuff in memory as that is much faster.
but the next skips a random number of lines with a max of 15.
have the idea that this might skip the 1st line but it's up 2 you 2 solve that (easy do).
for i=1,random(15) do
   read("*l")
end
line = read()

plop

thx plop i will give it a shot :))