Main Content

Splitting the difference

Archive - Originally posted on "The Horse's Mouth" - 2005-10-13 06:57:59 - Graham Ellis

Perl's split function takes a string of text, and divides it up at a delimiter of your choice into a list of shorter strings ... it's one of the "power tool" functions of Perl and a vital part of the language. So how come that you can write a Tcl program and use its version of split - or omit the split command - and it seems to work either way. Does that seem odd

In Tcl, the split function also turns a string into a list ... it's just that a Tcl list is a string that's formatted with an unprotected space between each list element, which is probably where you started from the in the first place. In other words, the reason you can often include or leave out the call is because in Tcl's case it often does nothing at all!!

Python has two split methods - one that uses an explicit string as the delimiter on which it's to split, and the other in its regular expression class that makes it much more closely akin to the Perl flavour, and in PHP the split function splits at a literal string (you should use preg_split or explode in PHP to split at a regular expression).

Do you ever wonder why I only teach one language per day?