Lua String Libary Quiz

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
undef
Party member
Posts: 438
Joined: Mon Jun 10, 2013 3:09 pm
Location: Berlin
Contact:

Lua String Libary Quiz

Post 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!
twitter | steam | indieDB

Check out quadrant on Steam!
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Lua String Libary Quiz

Post 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.
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
User avatar
NickRock
Citizen
Posts: 76
Joined: Thu Dec 25, 2014 9:33 pm
Location: Earth
Contact:

Re: Lua String Libary Quiz

Post 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
Weeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeooow!!
User avatar
Beelz
Party member
Posts: 234
Joined: Thu Sep 24, 2015 1:05 pm
Location: New York, USA
Contact:

Re: Lua String Libary Quiz

Post 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

Code: Select all

if self:hasBeer() then self:drink()
else self:getBeer() end
GitHub -- Website
User avatar
undef
Party member
Posts: 438
Joined: Mon Jun 10, 2013 3:09 pm
Location: Berlin
Contact:

Re: Lua String Libary Quiz

Post by undef »

Yeah, what then?
What's the desired output?
What's the challenge?
twitter | steam | indieDB

Check out quadrant on Steam!
User avatar
Beelz
Party member
Posts: 234
Joined: Thu Sep 24, 2015 1:05 pm
Location: New York, USA
Contact:

Re: Lua String Libary Quiz

Post 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)

Code: Select all

if self:hasBeer() then self:drink()
else self:getBeer() end
GitHub -- Website
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests