Main Content

The dog is not in trouble

Archive - Originally posted on "The Horse's Mouth" - 2009-07-17 19:58:20 - Graham Ellis

"I know I put my papers somewhere" I said to Lisa [wife], and Gypsy [dog] goes off and whimpers in the corner, looking very guilty.

So had she [dog] taken the papers and chewed them? That wasn't the case - she had heard the word "no" (or rather "know") in what I said, and had taken it that she was being told off for something.

We heard words and take them in context - they mean different things depending on the rest of the sentence surrounding them. "We don't subsidise lunches" could mean 'as a matter of policy we will never pay part of the cost of your lunch', or it could mean 'as it happens, we don't at the moment make any payment towards your lunch in our canteen'. And - taken out of context - this is an awfully good way of manipulating a message (or a naughty way, depending on your print of view).

It may surprise newcomers to programming that context also comes in to computer languages - a simple ^ (caret character) means different things in different places, and it's far from unique in having different meanings,


print "Say something: "
saying = gets.chop
 
puts "That starts with W" if saying =~ /^W/
puts "That contains a character which is not a letter" if saying =~ /[^A-Za-z]/
print 160 ^ 54, " shows the bitwise exclusive or operator\n"


So that's "Starts With" or "Anything Except" or "Either but not Both depending on what else is written around it. The example I have given is in Ruby, but the three meanings are the same in many other languages - thank goodness for that consistency, at least!

Running that code ...

Dorothy-2:jul09 grahamellis$ ruby csdp
Say something: Wicked
That starts with W
150 shows the bitwise exclusive or operator
Dorothy-2:jul09 grahamellis$ ruby csdp
Say something: Sweet Summer Sunshine
That contains a character which is not a letter
150 shows the bitwise exclusive or operator
Dorothy-2:jul09 grahamellis$


Some languages such as Perl make a very heavy use of context - if I write @abc, I could be referring to all the elements of a list, to the length of the list, or to the list expanded into a string. See article on context in Perl.

While checking this article, Google found me a rather nice Ruby Quick Reference - enjoy!