Okay, so for the past few days I’ve been playing with some binary data that I wasn’t looking forward to. It wasn’t the data so much as the last time I dealt this this particular type of data was in LISP and I was not happy with the results. In fact that experience was one of the major ones that got me to switch from LISP to Erlang.

When I started working with the binary data in Erlang I went back and re-read the documentation. It’s amazing how much easier the documentation is to read after working with Erlang for hundreds of hours, but I digress. After looking at the binary matching syntax I began to realize exactly how powerful it is and the project that I was working on was up and running in about 3 hours instead of my LISP attempt where I gave up on it after 3 days.

Then I started to realize where I could use binary pattern matching in my other application, specifically any where I use IP addresses. I often store IP addresses in their 32 bit decimal notation where the IP address 192.168.1.1 would turn out to be 3232235777. It’s easier to store and sort in databases that way, but how to convert from an IP Address tuple into an IP decimal.

With pattern matching it’s simple:

IPTuple = {192,168,1,1},
IPList = tuple_to_list(IPTuple),
IPBinary = list_to_binary(IPList),
<<IPDecimal:32>> = IPBinary,
IPDecimal.

believe me I was going through much more work to get this done when I wrote the original code for Spam Free Email to do this. I had no idea that I could do the whole thing with built in functions and I can’t wait to rewrite that section of code.

If you are doing anything with binary, or even if you are looking for simpler ways to do binary math, you really need to take a close look at Erlang’s binary pattern matching … really.

Update:

Joe Armstrong emailed me with a better version of the IP to decimal conversation. It uses more of the native binary pattern matching and I like it better then the one I wrote. Here it is:

IPTuple = {192,168,2,3},
{A,B,C,D} = IPTuple,
<<IPDecimal:32>> = <<A:8,B:8,C:8,D:8>>

Thanks Joe!