McDeKi Posted March 9, 2014 Share Posted March 9, 2014 Hello, I have a problem, I'm looping through all elements in my table. function SprawdzWolneMiejsca() for k, v in pairs ( Plecak ) do for i=1, wysokoscY do if not Plecak[k][i] == false then liczbaPrzedmiotow = liczbaPrzedmiotow + 1 outputChatBox(liczbaPrzedmiotow) return liczbaPrzedmiotow end end end end It works and output count of elements in table, but If I will use return then it output 1, because the loop wont ended looping through all elements, any advice how to fix it? I have tried doing, that when the loop is ending looping then I return value function SprawdzWolneMiejsca() for k, v in pairs ( Plecak ) do for i=1, wysokoscY do if not Plecak[k][i] == false then liczbaPrzedmiotow = liczbaPrzedmiotow + 1 outputChatBox(liczbaPrzedmiotow) if k == liczbaPrzedmiotow then return liczbaPrzedmiotow end end end end end Link to comment
Bonsai Posted March 9, 2014 Share Posted March 9, 2014 Just return outside of the for loop, I guess. Link to comment
teQuilla Posted March 9, 2014 Share Posted March 9, 2014 Maybe I'm asking to much , but can you please translate it into english , so we have an idea what you want to do there ? Link to comment
pa3ck Posted March 9, 2014 Share Posted March 9, 2014 I think he wants to break the loop as soon as it finds a result, if so, just use 'break' Link to comment
McDeKi Posted March 9, 2014 Author Share Posted March 9, 2014 (edited) I made a table full of falses { false false false false item item item false false } and I want to count how many items is in the table and then I want output count of items, that why I'm using return Edited March 9, 2014 by Guest Link to comment
Castillo Posted March 9, 2014 Share Posted March 9, 2014 function SprawdzWolneMiejsca ( ) local result = false for k, v in pairs ( Plecak ) do for i = 1, wysokoscY do if ( not Plecak [ k ] [ i ] == false ) then liczbaPrzedmiotow = ( liczbaPrzedmiotow + 1 ) outputChatBox ( liczbaPrzedmiotow ) if ( k == liczbaPrzedmiotow ) then result = liczbaPrzedmiotow break end end end end return result end Link to comment
McDeKi Posted March 9, 2014 Author Share Posted March 9, 2014 Ok, fixed it. function SprawdzWolneMiejsca ( ) liczbaPrzedmiotow = 0 for k, v in pairs ( Plecak ) do for i = 1, wysokoscY do if ( not Plecak [ k ] [ i ] == false ) then liczbaPrzedmiotow = ( liczbaPrzedmiotow + 1 ) end end end return liczbaPrzedmiotow end Thanks for advice. Link to comment
Moderators Citizen Posted March 9, 2014 Moderators Share Posted March 9, 2014 Ok, fixed it. function SprawdzWolneMiejsca ( ) liczbaPrzedmiotow = 0 for k, v in pairs ( Plecak ) do for i = 1, wysokoscY do if ( not Plecak [ k ] [ i ] == false ) then liczbaPrzedmiotow = ( liczbaPrzedmiotow + 1 ) end end end return liczbaPrzedmiotow end Thanks for advice. Please use local variables as soon as you can: local liczbaPrzedmiotow = 0 Link to comment
Castillo Posted March 9, 2014 Share Posted March 9, 2014 Ok, fixed it. function SprawdzWolneMiejsca ( ) liczbaPrzedmiotow = 0 for k, v in pairs ( Plecak ) do for i = 1, wysokoscY do if ( not Plecak [ k ] [ i ] == false ) then liczbaPrzedmiotow = ( liczbaPrzedmiotow + 1 ) end end end return liczbaPrzedmiotow end Thanks for advice. I first thought you wanted this, but then I didn't understand why you had this: if ( k == liczbaPrzedmiotow ) then Link to comment
McDeKi Posted March 9, 2014 Author Share Posted March 9, 2014 I know, but I put it as global only for testing. Link to comment
Moderators Citizen Posted March 9, 2014 Moderators Share Posted March 9, 2014 I know, but I put it as global only for testing. This make no sense, you can't test anything more by puting it as a global Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now