winded wrote:Hi everyone!
So first a disclaimer - I'm super new to coding. Lua is the first language I've ever tried to learn. However, a lot of the tutorials and posts that you guys have on the forum have been super helpful - especially the ones from Kikito.
Hi! Totally glad you've decided to learn programming and chose Lua to start.
winded wrote:I just recently finished *almost* all of the lua_missions that he has up in Github, and I have a few questions about them:
1. I don't know any regular expressions, so I skipped the second half of patterns.lua. Is there a good place to learn them? Do I need to know them right away, or should I just wait and learn them if I need? What are they used for?
Regular expressions are complex to people new to the concept, and then a very easy tool for people who understand them. You may want to return to them later if they're proving to be difficult. You can still go very far without them. But I'd advise to still try to learn them, as they're very handy.
What regular expressions, and Lua's patterns, are good for is looking at a string that is decidedly "regular" and pull out regular pieces of it. Like, as a for instance, these sentences are regular. They contain words separated by spaces and punctuation marks. Now, here are some messages that aren't like my sentences:
Oct 17 08:59:00 suod newsyslog[6215]: logfile turned over
Oct 17 08:59:04 cdr.cs.colorado.edu amd[29648]: noconn option exists, and was turned on! (May cause NFS hangs on some systems...)
Oct 17 08:59:09 freestuff.cs.colorado.edu ftpd[4502]: FTP ACCESS REFUSED (anonymous password not rfc822) from sdn-ar-001nmalbuP302.dialsprint.net [168.191.180.168]
Oct 17 08:59:24 peradam.cs.colorado.edu sendmail[21601]: e9HExOW21601: SYSERR(root): Can't create transcript file ./xfe9HExOW21601: Permission denied
With regular expressions, a program could tell the difference between my sentences and syslog messages, and pull out useful pieces.
winded wrote:2. I'm really thrown by metatables. In particular, I don't understand why in lines 66 and 75 of indices.lua, t2.x returns nil. It was just set to equal 1. That makes it seem like tables that have been associated with metatables can't be directly altered. Is that only the case when you're using the __newindex metamethod? Why?
__newindex means "There is a new index in the table", and that lets you do something with the value instead of the default action. It's not that putting a metatable on another table does that automatically, it's that the metatable will let you set a __newindex which will define it. In that mission, Kikito is trying to show you that the __newindex function is saying that trying to set something on t2 will instead put it on t1. But a different __newindex function could do something entirely different.
winded wrote:3. I'm completely lost on exercise.lua. First, I don't understand why we're trying to use a metamethod on strings instead of tables, when the previous lesson was all about using them on tables. Do they work on strings? How do you set them up to work with strings? Second, it seems like you should be able to solve this with a simple function. For the first set, why wouldn't you just use this sort of thing:
Code: Select all
local function starts_with(input_string, snippet)
return input_string:sub(1, #snippet) == snippet
end
The ends_with function would be really similar, just using #input_string to count backwards. These don't actually work, so I assume I'm approaching it wrong, since the hint is to use a metatable.
So this one is a bit more subtle. You can set any metatable on any table, but only per. One table can't really have two metatables, not without some kind of trickery with __index and __newindex. But, every table can have a different metatable. string, and some of the other simple types, can also have a metatable, EXCEPT that all strings must have the same metatable. You can't have two strings with different metatables. You can change which metatable it is with debug.setmetatable, but I'd advise against it. It's not a very useful thing to do. The default metatable for strings has already been set, and it has the most important metamethod set already (__index, pointing at the string library). In that exercize, kikito is showing you that by changing the string library table, all strings will have this new method on them.
Though, I agree with you that the simple function is the better way to go. And, actually kikito would agree with you that the simple function is better, as changing the default library tables isn't a very friendly thing to do. It could cause strange behavior in any code you may be using from other sources.
winded wrote:Again, I'm sorry if these questions are silly. I'm learning a TON, but I'm clearly still very much a beginner.
Thanks!
They're not silly at all, and you've done the most important thing that people new to programming can do, which is to ask when you have questions. Good luck!