Log In  

Hi

Could someone kindly tell me how to for loop through each char of a string? I'm unsure how this is done with pico 8.

P#94031 2021-06-25 19:23

given string s,

for i=1,#s do
  local c = sub(s, i, i)
  -- do stuff with c
end

I put "local" here because it's typically faster that way, but it is optional.

P#94034 2021-06-25 19:38

kimiyoribaka, many thanks!!

P#94035 2021-06-25 20:16

By faster, do you mean 'local' executes faster as code?

P#94036 2021-06-25 20:16

Yes, though I'm basing that claim on lua in general more than Pico-8. From what I've read, local variables in general tend to be faster to access. My guess is it's because underlying code has to wade through all the variables and functions available in the scope being used, and local scope has less to wade through.

P#94038 2021-06-25 21:18

Very good to know.
I will definitely keep that in mind. Thank you!

P#94039 2021-06-25 21:19

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-04-19 20:31:53 | 0.007s | Q:17