
Almost every program I've ever written (and almost every program you'll ever write) is required to output a mixture of constant character strings and results - formatting the output into a human readable form, or adding in separators between values in data files which the next program in a chain will read. So it's no surprise that there are lots of ways of doing the job ... here are some examples from Ruby.
1. Sending each of the expressions (be it a constant, a variable, or an expression) to the language's print function as a list of elements for the print statement to join up:
print("Henry is ",henry," and Jenny is ",jenny," and the difference is ",$diff,"\n")
2. Embedding the variables within the language's string construct using a syntax provided by the language for doing so:
puts("Henry is #{henry} and Jenny is #{jenny} and the difference is #{henry-jenny}")
3. Converting all of the values to be output into strings, then building up a complete string in an expression or variable which is outout:
puts("Henry is "+henry.to_s+" and Jenny is "+jenny.to_s+" and the difference is "+$diff.to_s)
4. Using a formatted printing function, with a single string to be output which includes placeholders, and additional parameters to fill in those placeholders:
printf("Henry is %d and Jenny is %d and the difference is %d\n",henry,jenny,$diff)
Of course, someone's going to ask "which one is best", and I'm going to say "it depends".
Option 1 - a whole list of things to output - is a very messy syntax with lots of "," to go wrong, and is hard to read. So it's not my favorite
Option 4 formats AND prints at the same time, and the computer science purists will tell you that a function should only do one thing at a time - so you'll be better to use an
sprintf to do the formatting followed by a
puts or print to do the output.
Option 3 is the way you'll find it done in languages such as Java - using an overloaded
+ operator to join strings, and a conversion method to convert numeric values into strings prior to joining them. It tends to be a bit verbose, but it's thorough - so longer to initially code and more straightforward to maintain.
Option 2 - where variables are embedded within a string - is the neatest. It's not available in all languages - in Ruby, you can see the syntax above. In Perl and PHP, where variables always start with a particular special character which the " operator understands (usually $, sometime @ in Perl), there's no need for the signatory #{..to..} as it's implicit in the variable name:
print("Henry is $henry and Jenny is $jenny and the difference is $diff\n");
There's a full example showing the options above
[here]. And the sample output in each case looks like
Henry is 34 and Jenny is 21 and the difference is 13 (written 2010-09-29)
Associated topics are indexed under
R106 - Input and Output in Ruby [3429] Searching through all the files in or below a directory - Ruby, Tcl, Perl - (2011-09-09)
[2893] Exclamation marks and question marks on ruby method names - (2010-07-28)
[2621] Ruby collections and strings - some new examples - (2010-02-03)
[2614] Neatly formatting results into a table - (2010-02-01)
[2290] Opening and reading files - the ruby fundamentals - (2009-07-16)
[1887] Ruby Programming Course - Saturday and Sunday - (2008-11-16)
[1587] Some Ruby programming examples from our course - (2008-03-21)
Some other Articles
Christmas 2010 - Well House Manor, Melksham, HotelWhat is a factory method and why use one? - Example in RubyCreating, extending, traversing and combining Ruby arraysWhy do I need brackets in Ruby ... or Perl, Python, C or JavaFormatting your output - options available in RubyLearning to program - where to start if you have never programmed beforeSome more advanced Perl examples from a recent courseShould the public sector compete with businesses? and other deep questionsPerl - doing several things at the same timeWhat does blessing a variable in Perl mean?