foxygit / fake-08-xbox360 Log in
commit 9151735bd4f83418cae27e91cdb7854b4b620862
Author:     Godmil <godmil@gmail.com>
AuthorDate: Thu Jul 6 11:08:24 2023 +0100
Commit:     GitHub <noreply@github.com>
CommitDate: Thu Jul 6 11:08:24 2023 +0100

    Update p8GlobalLuaFunctions.h

    Added extra functionality to count() as described in the pico8 manual.
---
 source/p8GlobalLuaFunctions.h | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/source/p8GlobalLuaFunctions.h b/source/p8GlobalLuaFunctions.h
index 5081dac..6d40bd3 100644
--- a/source/p8GlobalLuaFunctions.h
+++ b/source/p8GlobalLuaFunctions.h
@@ -39,12 +39,19 @@ end
 -- from returning #c in cases where the table is no longer an array. See
 -- the tables.p8 unit test cart for more details.
 --
+-- count() takes an optional value as its second argument, if this is present
+-- then count() will return the number of times the value is found in the table.
+--
 -- We also try to mimic the PICO-8 error messages:
 --  count(nil) → attempt to get length of local 'c' (a nil value)
 --  count("x") → attempt to index local 'c' (a string value)
-function count(c)
+function count(c,v)
     local cnt,max = 0,#c
-    for i=1,max do if (c[i] != nil) cnt+=1 end
+    if v == nil then
+        for i=1,max do if (c[i] != nil) cnt+=1 end
+    else
+        for i=1,max do if (c[i] == v) cnt+=1 end
+    end
     return cnt
 end