August 11, 2003

BuildNumberAreGood

Filed under: ResearchAndDevelopment — Ryan Wilcox @ 1:14 am

I spent most of the afternoon on Friday writing a Perl script to automatically keep track of build-numbers for me. Every time a build happens, the number goes up by one.

Perl happens to have regular expressions built right into the language. Meaning you can do fun stuff like:


$line =~ m/(\d.*)/; #match the following grep pattern to the line variable

Pretend your input line is something like this:

That’s all you need to do to match a grep pattern.

Of course, then you start using regular expressions for everything. You’ve got this nifty hammer, so, duh, everything must be a nail, right?

In Python I wouldn’t have used grep, in this case. Lemme tell you what I was trying to do…

Say you have an input line like this:


#define kBuildNumber "1"

And you want to increment the value for kBuildNumber. In Perl I used regular expressions to find kBuildNumber in the string (because I might have other stuff – like comments – in the file).

If kBuildNumber was in that line I then used grep to find my number.

In Python I would have saved the position of kBuildNumber in the string, and kept every character after that string stopped. I would have then stripped away the white space, avoided the quotes, and incremented the number.


step1 = myLine[endOfConstant:]
#from where the constant ends to end of line

step2= step1.strip()
#strip whitespace characters out

number = step2[1:-1]
#we have starting an ending quotes to worry about.

It might not be as compact as a regular expression, and yes, implimenting the solution using regular expressions in Python would be a simple matter of:


myResult = re.compile('\d.*').match(line).

It just felt like I really didn’t need regular expressions for this problem. Maybe I did… but the time it took my to debug and perfect my grep pattern was way more then it would have taken to use Python’s cool splicing operations (that’s what the myLine[x:n] stuff is called).

But I knew exactly where the number was: it started the second character after the whitespace after the constant name. It’s always going to be there – I can pin-point this thing down fast, I don’t need grep trying to figure it all out for me.

Maybe it is more elegant with regular expressions… I just don’t think so.

No Comments

No comments yet.

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.