Saturday, December 15, 2007

Pattern matching

This week I used for my first time (at least seriously) the pattern matching functions of Lua. I am amazed of how powerful they are! I made two programs using them.
  1. A program that parses the stdout of the finger command (which is issued for the name of all subdirectories of /home/ ) and then issues an id command for each user on the server of my university. This way I made a 250KB textfile containing usernames, realnames, registration numbers, emails and years of registration; just by organizing information that is normally available by using these two commands. Of course this program in it's present form works correctly only for my university...
  2. A simple crawler. After making the main crawler part I tried to find something more useful to collect than emails but I came up with nothing so I made it seek email addresses. (but I hate spam :-P )
Here is an example:
for u,n in string.gmatch(data,"Login: (%a+)%s+Name: (%a+ *%a*)\n") do This is how you start a for loop that will iterate for each username and realname pair that the output of the finger command contains. I uploaded both files (along with Lua interpreters for Linux and Windows and the Luasocket which is needed by the crawler) and you can get them by clicking here. In case you want to run the first program you should upload steal.lua and lua5.1 to a Unix system and then run something like this: ./lua5.1 steal.lua To run the crawler simply drag crawler on the appropriate Lua interpreter.

2 comments:

  1. I'm amazed at how poorly documented they are. Or perhaps how poorly the documentation is organized.

    They may have made something neat with Lua pattern matching, but pcre remains the best, if only because it's much easier to find out how to use it ;-)

    ReplyDelete
  2. I kinda like Lua's documentation. It is small anyway so I siply read it all and remember most of the function, then I only come back to read about patters (ctrl+f "patterns") etc...
    ;-)

    I don't know pcre but I will give it try. :-)

    ReplyDelete

Popular Posts