• Print

Author Topic: Learning Lua... (Just a random post from a lua super noob.)  (Read 27855 times)

0 Members and 1 Guest are viewing this topic.

Offline syn.

  • Jr. Member
  • **
  • Posts: 56
  • Karma: 6
  • Lua Padawan
    • SynGaming
Re: Learning Lua... (Just a random post from a lua super noob.)
« Reply #45 on: January 26, 2014, 03:15:42 pm »
I haven't added anything I've made myself to the server besided some really simple custom commands because TTT is confusing and I get stuck on where to put certain things and I don't really know much about gmod lua in general, also I can't seem to get ipairs :( I'll keep trying but any small hints on Jam's puzzle would be awesome:)
Lua Student

"The more you understand, the crazier you get."

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Learning Lua... (Just a random post from a lua super noob.)
« Reply #46 on: January 26, 2014, 03:31:24 pm »
ipairs = indexed pairs
k = (index) key
v = value
ipairs gives you both (a pair) from the table parts.
The for loop goes through the table and assigns each variable (k, v) the respective key and value.

"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline syn.

  • Jr. Member
  • **
  • Posts: 56
  • Karma: 6
  • Lua Padawan
    • SynGaming
Re: Learning Lua... (Just a random post from a lua super noob.)
« Reply #47 on: January 26, 2014, 06:02:53 pm »
ohh okay so k would be the third key in the table (20) and value would be it's value?
If so then it would be: "I found 20!"
Lua Student

"The more you understand, the crazier you get."

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Learning Lua... (Just a random post from a lua super noob.)
« Reply #48 on: January 26, 2014, 06:07:40 pm »
ohh okay so k would be the third key in the table (20) and value would be it's value?
If so then it would be: "I found 20!"
Partially correct. There's more output than that.
What would all the output be?
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline syn.

  • Jr. Member
  • **
  • Posts: 56
  • Karma: 6
  • Lua Padawan
    • SynGaming
Re: Learning Lua... (Just a random post from a lua super noob.)
« Reply #49 on: January 26, 2014, 06:19:15 pm »
Code: Lua
  1. 1   0
  2. 2   100
  3. 3   20
  4. 4   99
  5. I found 20!
because you are printing all k, v's and then afterward you are printing I found 20!
Lua Student

"The more you understand, the crazier you get."

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Learning Lua... (Just a random post from a lua super noob.)
« Reply #50 on: January 26, 2014, 08:14:05 pm »
Close.
The if statement is inside the for loop, before the for loop finishes.
It would look like;
    1   0
    2   100
    3   20
    I found 20!
    4   99
Now, a small lesson.
Placing that if statement outside the for loop would do nothing, wouldn't print anything.
For loops are 'local'. You can't use information/variables created within the actual 'for' statement unless you assign them to a variable outside the scope.
k would be nil outside. v would be nil. nil ~= 3, so nothing would be printed of the 'i found' line (and v would be nil too)
To make the code do the output like you originally said it would look like would be like this;
Code: [Select]
x = { 0, 100, 20, 99 }
for k, v in ipairs( x ) do --k/v = local, x is a global (to this anyway) table
   print ( k, v )
   if k == 3 and v == 20 then c = 20 end --k/v = local, c = global
end
   if c == 20 then print ( "I found " .. c .. "!") end
(Oh, and though the 'and' comparison in the k= and v= is totally unnecessary (you could use 'if v == 20', I threw it in to show some logic operators.)
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline syn.

  • Jr. Member
  • **
  • Posts: 56
  • Karma: 6
  • Lua Padawan
    • SynGaming
Re: Learning Lua... (Just a random post from a lua super noob.)
« Reply #51 on: January 27, 2014, 05:18:56 pm »
Ohh okay thanks jam I mostly get confused on what is called what, for example I had no idea k and v were keys and values (even though in hindsight it seems a bit obvious).
Lua Student

"The more you understand, the crazier you get."

  • Print