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?
Some other articles
Y115 - Additional Python Facilities Some gems from Intermediate Python Command line parameter handling in Python via the argparse module Json load from URL, recursive display, Python 3.4 Running an operating system command from your Python program - the new way with the subprocess module Json is the new marshall, pickle and cPickle / Python Python - an interesting application Handling JSON in Python (and a csv, marshall and pickle comparison) JSON from Python - first principles, easy example Teaching dilemma - old tricks and techniques, or recent enhancements? A demonstration of how many Python facilities work together Python regular expressions - repeating, splitting, lookahead and lookbehind Joining a MySQL table from within a Python program Factory methods and SqLite in use in a Python teaching example Running operating system commands from your Python program Python decorators - your own, staticmethod and classmethod Model - View - Controller demo, Sqlite - Python 3 - Qt4 Connecting Python to sqlite and MySQL databases Regular Expressions in Python Python - what is going on around me? Python - how it saves on compile time Serialization - storing and reloading objects Testing code in Python - doctest, unittest and others Python Regular Expressions A series of tyre damages Ignore case in Regular Expression Regular expressions made easy - building from components Turning objects into something you can store - Pickling (Python) Buffering output - why it is done and issues raised in Tcl, Perl, Python and PHP Sending an email from Python Python - listing out the contents of all variables Python 3000 - the next generation Keeping your regular expressions simple Python to MySQL This article What and why for the epoch Examples - Gadfly, NI Number, and Tcl to C interface The elegance of Python Y108 - String Handling Prining a pound sign from Python AND running from the command line at the same time Python formatting update - including named completions Command line parameter handling in Python via the argparse module Python - comparison of old and new string formatters Identifying and clearing denial of service attacks on your Apache server Formatting options in Python Why are bus fares so high? Collections in Python - list tuple dict and string. Formatting output - why we need to, and first Python example Backquote, backtic, str and repr in Python - conversion object to string Teaching dilemma - old tricks and techniques, or recent enhancements? Python string formatting - the move from % to str.format Formatting output in Python through str.format Matching a license plate or product code - Regular Expressions Matching to a string - what if it matches in many possible ways? Python - splitting and joining strings Formatted Printing in Python Running operating system commands from your Python program Regular Expressions in Python Flexible search and replace in Python Pound Sign in Python Program Strings as collections in Python Python Regular Expressions Underlining in Perl and Python - the x and * operator in use Python - formatting objects Regular Express Primer Python - two different splits String duplication - x in Perl, * in Python and Ruby Splitting Pythons in Bradford Matching within multiline strings, and ignoring case in regular expressions Pieces of Python Breaking bread The fencepost problem Python printf This article The backtick operator in Python and Perl T206 - Lists Working out distance between places, using OS grid references and a program in Tcl Everything is a string - even a list Lists in Tcl - fundamentals in a commented source code example lists and struct::list in Tcl - Introduction to struct::list and examples Expanding a list of parameters in Tcl - {*} and eval Tcl collections - lists, dicts and array User defined sorting and other uses of callbacks in Tcl and Tk The difference between lists and strings - Tcl Extracting data from a string / line from file - Tcl split and join in tcl and expect What are Tcl lists? Replacing the last comma with an and Sorting in Tcl - lists and arrays Tcl - append v lappend v concat Stable sorting - Tcl, Perl and others Generating traffic for network testing Stringing together Tcl scripts Tcl - lappend v concat This article Tcl sandwich - lists in Tcl P208 - Lists in Perl Mapping an array / list without a loop - how to do it in Perl 6 Lots of ways of doing the same thing in Perl - list iteration Taking the lead, not the dog, for a walk. Writing more maintainable Perl - naming fields from your data records Stepping through a list (or an array) in reverse order Dark mornings, dog update, and Python and Lua courses before Christmas $ is atomic and % and @ are molecular - Perl Copying - duplicating data, or just adding a name? Perl and Python compared Fresh Perl Teaching Examples - part 2 of 3 Iterating over a Perl list and changing all items Finding text and what surrounds it - contextual grep The dog is not in trouble Revision / Summary of lists - Perl Perl - lists do so much more than arrays Perl Socket Programming Examples Out of memory during array extend - Perl Perl - map to process every member of a list (array) Perl ... adding to a list - end, middle, start Filtering and altering Perl lists with grep and map Last elements in a Perl or Python list Perl - a list or a hash? C++ and Perl - why did they do it THAT way? Breaking bread Huge data files - what happened earlier? Queues and barrel rolls in Perl The fencepost problem This article Context in Perl Conventional restraints removed Course sizes - beware of marketing statistics Comparison Chart for Perl programmers - list functions Perl for breakfast H107 - String Handling in PHP Splitting the difference with PHP Setting up strings in PHP Solution looking for a problem? Lookahead and Lookbehind More than just matching with a regular expression in PHP Getting more than a yes / no answer from a regular expression pattern match Learning to program in PHP - Regular Expression and Associative Array examples Regular Expression modifiers in PHP - summary table PHP - moving from ereg to preg for regular expressions Divide 10000 by 17. Do you get 588.235294117647, 588.24 or 588? - Ruby and PHP Handling (expanding) tabs in PHP Curly braces within double quoted strings in PHP Handling nasty characters - Perl, PHP, Python, Tcl, Lua Making Regular Expressions easy to read and maintain Finding variations on a surname Regular Expressions in PHP Regular expression for 6 digits OR 25 digits Do not SHOUT and do not whisper Short and sweet and sticky - PHP form input A taster PHP expression ... Ignore case in Regular Expression Regular Express Primer PHP Regular expression to extrtact link and text Date conversion - PHP Looking ahead and behind in a Regular Expression Evaluating arithmetic expressions in configuration files How similar are two words Don't expose your regular expressions Robust PHP user inputs PHP - dividing a string up into pieces The fencepost problem Converting between acres and hectares Running a Perl script within a PHP page This article PHP Magic Quotes the array returned by preg_match_all PHP and natural sorting Here documents