McDeKi Posted March 9, 2014 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
teQuilla Posted March 9, 2014 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 ?
pa3ck Posted March 9, 2014 Posted March 9, 2014 I think he wants to break the loop as soon as it finds a result, if so, just use 'break'
McDeKi Posted March 9, 2014 Author 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
Castillo Posted March 9, 2014 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
McDeKi Posted March 9, 2014 Author 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.
Moderators Citizen Posted March 9, 2014 Moderators 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
Castillo Posted March 9, 2014 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
McDeKi Posted March 9, 2014 Author Posted March 9, 2014 I know, but I put it as global only for testing.
Moderators Citizen Posted March 9, 2014 Moderators 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
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