Navydev
|
  |
| Joined: 21 Feb 2012 |
| Total Posts: 274 |
|
|
| 26 Sep 2013 05:19 PM |
So, I know the basics of them, and going through objects in general, but for the life of me I don't understand why my table stops printing halfway through. The script:
Actors = {"Tyler","Darian","Iggy","Cameron","Bailey","Dominick","Kat","Steve","Daniel"} c = 0 print("---------------------------------------------------------------------------------------------------------------------------------------------------") for _, p in pairs(Actors) do wait(.25) r = math.random(1,#Actors) if c == 0 then print("Bottom is worked by: "..Actors[r]) table.remove(Actors,r) elseif c == 1 then print("Second is worked by: "..Actors[r]) table.remove(Actors,r) elseif c == 2 then print("Third is worked by: "..Actors[r]) table.remove(Actors,r) elseif c == 3 then print("and worked by: "..Actors[r]) table.remove(Actors,r) elseif c == 4 then print("Top is worked by: "..Actors[r]) table.remove(Actors,r) elseif c == 5 then print("Bridge is worked by: "..Actors[r]) table.remove(Actors,r) elseif c == 6 then print("Infirm is worked by: "..Actors[r]) table.remove(Actors,r) elseif c >= 7 then print("Preliminary people assigned, here are the last remaining "..(#Actors).." people: "..returning()) end c = c + 1 end
function returning() local cc = 0 for _, pp in pairs(Actors) do cc = cc + 1 print(Actors[cc]) end end
--------------
The Output: --------------------------------------------------------------------------------------------------------------------------------------------------- Bottom is worked by: Iggy Second is worked by: Tyler Third is worked by: Bailey and worked by: Cameron Top is worked by: Daniel
What do I do? Any suggestions or details to make this cleaner? I know for a fact that the constant "if" statements are a mess, there has to be a way to make that neater... |
|
|
| Report Abuse |
|
|
Navydev
|
  |
| Joined: 21 Feb 2012 |
| Total Posts: 274 |
|
| |
|
Salinas23
|
  |
| Joined: 28 Dec 2008 |
| Total Posts: 37142 |
|
|
| 26 Sep 2013 06:06 PM |
Expect an answer not soon...
I have a spider on my back. |
|
|
| Report Abuse |
|
|
Navydev
|
  |
| Joined: 21 Feb 2012 |
| Total Posts: 274 |
|
|
| 26 Sep 2013 06:22 PM |
| Owtch, hope it doesnt bite you |
|
|
| Report Abuse |
|
|
databrain
|
  |
| Joined: 01 Jan 2013 |
| Total Posts: 3342 |
|
|
| 26 Sep 2013 06:31 PM |
function returning() local cc = 0 for _, pp in pairs(Actors) do cc = cc + 1 print(Actors[cc]) end end
Change this to
function returning() for cc, pp in ipairs(Actors) do print(pp) end end
Ipairs is what your looking for.
The l0z3r p00p3r of ATR |
|
|
| Report Abuse |
|
|
Geomaster
|
  |
| Joined: 05 Jul 2008 |
| Total Posts: 1480 |
|
|
| 26 Sep 2013 06:38 PM |
Use another table to clean up those bulky 'Ifs'
Actors = {"Tyler","Darian","Iggy","Cameron","Bailey","Dominick","Kat","Steve","Daniel"} Stages = {"Bottom", "Second", "Third", "Top", "Bridge", "Infirm"} local c = 1 print("---------------------------------------------------------------------------------------------------------------------------------------------------") for _, p in pairs(Actors) do wait(.25) r = math.random(1,#Actors) if c <= #Stages then print(Stages[c] .." is worked by: " ..p table.remove(Actors[r]) elseif c > #Stages then print("Preliminary people assigned, here are the last remaining "..(#Actors).." people: "..returning()) end c = c + 1 end
|
|
|
| Report Abuse |
|
|
databrain
|
  |
| Joined: 01 Jan 2013 |
| Total Posts: 3342 |
|
|
| 26 Sep 2013 06:39 PM |
Anyways, instead of using C, use Ipairs.
The l0z3r p00p3r of ATR |
|
|
| Report Abuse |
|
|
Navydev
|
  |
| Joined: 21 Feb 2012 |
| Total Posts: 274 |
|
|
| 26 Sep 2013 06:54 PM |
How does Ipairs replace using a counter?
|
|
|
| Report Abuse |
|
|
Navydev
|
  |
| Joined: 21 Feb 2012 |
| Total Posts: 274 |
|
|
| 26 Sep 2013 07:00 PM |
Right, with some tweaks, the script is now:
Actors = {"Tyler","Darian","Iggy","Cameron","Bailey","Dominick","Kat","Steve","Daniel"} Stages = {"Bottom", "Second", "Third", "Third2", "Top", "Bridge", "Infirm"} local c = 1 print("---------------------------------------------------------------------------------------------------------------------------------------------------") for _, p in pairs(Actors) do wait(.25) r = math.random(1,#Actors) if c <=(#Stages) then print(Stages[c].." is worked by: "..p) table.remove(Actors,r) elseif c> (#Stages) then print("Preliminary people assigned, here are the last remaining "..(#Actors).." people: "..returning()) end c = c + 1 end
function returning() for cc, pp in ipairs(Actors) do print(Actors[pp]) end end
...However, it still stops counting at five, when it should instead iterate the amount of spots, before stopping at the remainder of people.
It still stops at 5 - what do?
.-. |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 26 Sep 2013 07:02 PM |
print(pp)
or
print(Actors[cc]) |
|
|
| Report Abuse |
|
|
Navydev
|
  |
| Joined: 21 Feb 2012 |
| Total Posts: 274 |
|
|
| 26 Sep 2013 09:42 PM |
| The mystery shall never be solved |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
| |
|
Navydev
|
  |
| Joined: 21 Feb 2012 |
| Total Posts: 274 |
|
|
| 27 Sep 2013 01:03 PM |
You didn't solve the original purpose of the script - in that It always stops halfway through. Even with this newer version of it, the problem is still the same, and so it is unsolved.
:S |
|
|
| Report Abuse |
|
|
databrain
|
  |
| Joined: 01 Jan 2013 |
| Total Posts: 3342 |
|
|
| 27 Sep 2013 07:56 PM |
for c,v in ipairs(table) do
c is the counter. It's built in with Ipairs
The l0z3r p00p3r of ATR |
|
|
| Report Abuse |
|
|