Saturday, December 15, 2007

Pattern matching

REACT! 
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.

Popular Posts