Main Content
Breaking bread Archive - Originally posted on "The Horse's Mouth" - 2006-06-22 07:17:45 - Graham Ellis
How can I take ONE thing, discard some of it, and end up with MANY things all of the same type as the original? Surely that doesn't make sense does it?
I could take a packet of half a dozen rolls from the supermarket, made (as they often are in the UK) as a single piece of bread with narrow "tear" points, and tear into six pieces. A few crumbs to be thrown away - and there you have it. Six things (of type "piece of bread") where the sum total is actually LESS that the one thing ("piece of bread") that I started with.
This action of breaking bread has a parallel in programming. In Perl (split), Python (split) and PHP (explode, split) amongst others, you can take a single string and break it down into a whole list (Perl, Python) / array (PHP) of smaller strings. You'll often want to do this if you have a string that's a line of data read from a file, and you want to break it down into a series of strings each of which is an individual field.
@pieces = split(/\s+/,$line); # Perl
Some other articles
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 This article The fencepost problem Python printf Splitting the difference The backtick operator in Python and Perl 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? This article Huge data files - what happened earlier? Queues and barrel rolls in Perl The fencepost problem Splitting the difference Context in Perl Conventional restraints removed Course sizes - beware of marketing statistics Comparison Chart for Perl programmers - list functions Perl for breakfast H106 - Arrays Disambiguation - PHP List Splitting the difference with PHP Arrays in PHP - contain different and even mixed data types Learning to program in PHP - Regular Expression and Associative Array examples Sorting data the way YOU want it sorted Increment operators for counting - Perl, PHP, C and others Sorting - naturally, or into a different order Looking up a value by key - associative arrays / Hashes / Dictionaries PHP preg functions - examples and comparision If nothing, make it nothing. When an array is not an array More PHP sample and demonstration programs Testing for one of a list of values. PHP adding arrays / summing arrays Displaying data at 5 items per line on a web page This article PHP - setting sort order with an associative array Functions and commands with dangerous names