Lua Comparison Operators (wildcard?)
lua, operators
Solution
You want the function `string.match()`, which tests if a string matches a pattern.
Here's my re-write of your example (untested):
--scan the directory's files
for file in lfs.dir(doc_path) do
--> look for any files ending with .jpg
if file:match "%.jpg$" then
--do something if any files ending with .JPG are scanned
end
end
The notation `file:match "%.jpg%"` calls the function `string.match` using the method call syntax sugar, which works because all string values have `string` set as their metatable by default. For simplicity of expression, I've also dropped the parenthesis around the sole parameter.
The pattern is anchored to the end of the string by the `$` at the end, and tests for a literal `.` by quoting it with `%`. However, since patterns are case sensitive, this is only matching files where the extension is all lower case.
To make it case-insensitive, the simplest answer is to fold the case of the file name before testing by writing `file:lower:match"%.jpg$"`, which chains a call to `string.lower()` before the call to `match`. Alternatively, you can rewrite the pattern as `"%.[Jj][Pp][Gg]$"` to match each character explicitly in either case.
Problem
I'm new to Lua, so I'm learning the operators part now. Is there a wildcard character that works with strings in Lua? I come from a PHP background and I'm essentially trying to code this: ``` --scan the directory's files for file in lfs.dir(doc_path) do --> look for any files ending with .jpg if file is like ".jpg" then --do something if any files ending with .JPG are scanned end end ``` You'll see I'm looking out for JPG files, while I'm looping through files in a directory. I'm used to the percentage sign, or asterisk character for searching strings. But perhaps Lua has a different way? Also, I'm totally guessing with the statement: "if file is like......."