The Barrettocracy
Barrett's Writings and Stuffs

Let's Split

Posted on February 16, 2008 at 01:30 PM

Compare and Contrast

I was planning on this next entry being a comparison of regular expressions between a few languages used in web development (Perl, PHP, Ruby, and Tcl). Tcl? Yes - it's baked into AOLServer, which we still use some at my Large Multinational Corporation. With this week being Valentine's Day, though, I was inspired to go in a different direction.

Let's split!

I decided to use an example I have used in the real world myself: split a series of key-value pairs (querystring data, for example) and put the data into a structure that makes it easy to get to the bits that you need.

But wait, you say. I can already get to the key-value pairs in my querystring. True. This is useful for parsing data out of a server log.

Tcl

Tcl is baked into AOLServer. It is a language that could be used as a form of torture, too. I've never liked it, and this will help illustrate why. This snippet will also not be privileged to have the fancy syntax highlighting.

set qs "var1=one&var2=two&var3=three"
set pairs [split $qs "&"]

foreach pair $pairs {
  set pair_list [split $pair "="]
  set key [lindex $pair_list 0]
  set value [lindex $pair_list 1]
  set hash($key) $value
}
puts $hash(var2)

Split creates a list. You can iterate over the list, but you can only get to the individual items one line at a time. There is an lassign method, but it doesn't work on my version of Tcl. You use lindex to get the individual elements. How much fun would this be if you were splitting the pieces of the date command? Woo hoo!

You do not have to initialize the hash, though.

PHP

Here is another language that makes me crazy. it should also be noted that this is not one that you can run from the command line as far as I can tell. Maybe you can, but why would you?

$qs="var1=one&var2=two&var3=three";
# preg_split -- not to be confused with split or str_split
$pairs=preg_split("/&/", $qs);

foreach ( $pairs as $pair ) {
  list($key, $value) = split("=", $pair);
  $hash{$key} = $value;
}
print $hash{"var2"};

There are at least 3 different split commands. WTF?!? You'll also note that preg_split returns an array, but split returns a list that has to be specifically defined. I find that keeping similar tasks as different as possible makes it a lot easier to get things done. Don't you?

Perl

$qs="var1=one&var2=two&var3=three";
@pairs=split(/&/, $qs);

foreach $pair ( @pairs ) {
  ( $key, $value ) = split(/=/, $pair);
  $hash{$key} = $value;
}
print $hash{"var2"};

You will note that I am a recovering perler. This still feels pretty good. Don't get me wrong. I still don't want to go touch any of the big sites I have in Perl. But for parsing through a logfile Perl is not a bad option.

Ruby

I've turned a new leaf, and am doing new sites in Rails. I'm also writing all of my new scripts in Ruby. It is a different world, and that's not a bad thing. The querystring parsing example may be a bit of a stretch for this. Here is my take on it:

qs="var1=one&var2=two&var3=three"
hash = Hash.new
qs.split(/&/).each do |pair|
  k, v = /(.+)\=(.+)/.match(pair).captures
  hash[k] = v
end
puts hash["var2"]

This uses MatchData to split the individual key-value pairs. This is one of the cool things with ruby. Everything is an object-- even regular expressions. The regex has the match method. You could take it a step further and define the regex separately.

In Conclusion

As I learn new things I find it helpful to see how it fits with what I know. This is an exercise I like to go through sometimes -- take a script that does some basic stuff and rewrite it in a different language. That way you can easily test yourself.

I'm interested to hear your experiences, or if you have a different way to accomplish any of these examples.

Tags: TechStuff
Hierarchy: previous, next

Comments

There is 1 comment on this post. Post yours →

Clearly you ARE a nerd and I am not since all of this is WAY above my head. However, I can vouch for you as a web designer... Anyone thinking on taking on this nerd- go for it. You will be pleased at teh results and bang for your buck.

Post a comment

Required fields in bold.