Page 1 of 1

Lua String Libary Quiz

Posted: Sat Dec 05, 2015 6:47 pm
by undef
A lot of people seem unexperienced with the string libary, so I thought I'd open a thread where people can find nice examples to practice.

Feel free to add challenges of your own, or give a better soloution to a previously solved puzzle.

Preferably make puzzles that have "real world application", so people can see how useful knowledge of the string libary is.


I'll go first:


Let's say you have a camera that saves your pictures in the following naming convention:

IMG_20151023_133742.jpg

Now create a program that would print out a string like this for every image file in a folder:

Picture taken at 13:37:42 on 23/10/2015.


Not that you could also easily make a program that renames all the files like this using os.rename.

Give it a shot!

Re: Lua String Libary Quiz

Posted: Sat Dec 05, 2015 8:56 pm
by davisdude

Code: Select all

local str = 'IMG_20151023_133742.jpg'
result = str:gsub( 'IMG_(%d%d%d%d)(%d%d)(%d%d)_(%d%d)(%d%d)(%d%d)%.jpg', 'Picture taken at %4:%5:%6 on %3/%2/%1'  )
Note that you could really do this a couple ways, I just wanted to demonstrate a pretty useful method.

Re: Lua String Libary Quiz

Posted: Mon Dec 07, 2015 1:26 pm
by NickRock
For me personally I don't have a problem learning some string functions, the thing that bothers me a lot is using Regular Expressions

Re: Lua String Libary Quiz

Posted: Tue Dec 08, 2015 12:05 am
by Beelz
Now what if the camera appends a letter to the end of the original string? For example, I have a DSLR camera with rapid capture functionality. It snaps 5 pictures per second and appends letters only to images that occur during the same second (with the first picture that second as an exception).

Example:
20151207220631.jpeg
20151207220631a.jpeg
20151207220631b.jpeg
20151207220632.jpeg
20151207220632a.jpeg
20151207220632b.jpeg
20151207220632c.jpeg

EDIT **

Or keep the same format as the first example just because:
IMG_20151207_220631.jpeg
IMG_20151207_220631a.jpeg
etc

Re: Lua String Libary Quiz

Posted: Tue Dec 08, 2015 3:13 pm
by undef
Yeah, what then?
What's the desired output?
What's the challenge?

Re: Lua String Libary Quiz

Posted: Tue Dec 08, 2015 6:35 pm
by Beelz
undef wrote:Yeah, what then?
What's the desired output?
What's the challenge?
Ummmmmmmm... Its basically the same as what you had originally asked, just added a final character to only certain files. The challenge I guess would be to do the same thing but to include that. Example output:

Picture taken at 22:05:35 on 12/07/2015

Picture taken at 22:06:31 on 12/07/2015
Picture taken at 22:06:31 on 12/07/2015 - (a)
Picture taken at 22:06:31 on 12/07/2015 - (b)

Picture taken at 22:06:32 on 12/07/2015

Picture taken at 22:06:34 on 12/07/2015
Picture taken at 22:06:34 on 12/07/2015 - (a)